nopSolutions/nopCommerce

Support required products with product has combinations

Closed this issue · 1 comments

nopCommerce version: All

Steps to reproduce the problem:
If you have a required product to be added automaticly when adding a specific product, it will not work. To solve this, i have added somecode to GetRequiredProductWarningsAsync method as follwoing:

//add to cart (if possible)
if (addRequiredProducts && product.AutomaticallyAddRequiredProducts)
{
// NEW CODE: if the products has attributes, then get the default one
string attributeXml = await GetProductAttribute(requiredProduct);

                //do not add required products to prevent circular references
                var addToCartWarnings = await AddToCartAsync(customer, requiredProduct, shoppingCartType, storeId,
                    quantity: quantityToAdd, addRequiredProducts: false, attributesXml: attributeXml);

                //don't display all specific errors only the generic one
                if (addToCartWarnings.Any())
                    warnings.Add(requiredProductWarning);
            }
            else
                warnings.Add(requiredProductWarning);

**private async Task GetProductAttribute(Product product)
{
var att = (await _productAttributeService.GetAllProductAttributeCombinationsAsync(product.Id)).Where(x => x.StockQuantity > 0).OrderBy(x => x.OverriddenPrice).FirstOrDefault();
if (att != null)
{
// This means the required product has attribute
return att.AttributesXml;
}

        return null;
    }**

Thanks a lot for your suggestion. But this is a default bahavior. A customer has to choose attributes if a product has it. So products with attributes should be added as required (added automatically).

We'll leave it for customization