3.90 Plugin Development questions (Product Attribute assignment through controller)

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
6 years ago
Hi, I'm fairly new to nopcommerce and tried to find something that could help guide me to doing this but found nothing for version 3.90.

The plugin routes to a customizable site where the user can customize say a t-shirt then routes the user back to nopcommerce but also adds the item to the cart. In this process I'm also trying to find a way to add a Product Attribute to the specific product id say t-shirt for the nopcommerce product so when say the same product like the t-shirt is customized more than once for the same cart, the product quantities don't get clumped into 1 single ShoppingCartItemId. I also want to insert a value automatically into that product attribute in order to save to my plugin's table where I'm storing an assortment of fields when inserting the record.

I'm not 100 percent where to go with this, I have tried the method within IProductAttributeService the InsertProductAttribute and then the ProductAttributeValue but I get nothing but errors stating the product table cannot insert the value.

Any example or help would be appreciated. Thank you
6 years ago
Hi,
Please let me know if I am wrong. As per your explanation you want to add product attribute (new row to 'ProductAttribute' table). you initialized 'IProductAttributeService' instance in your plugin controller and calling a method similar to _productAttributeService.InsertProductAttribute(productAttribute);  but it not adding value instead it is throwing error.

Could you please share your code sample
6 years ago
Thanks for responding. I ended up just adding the Product Attribute manually through the admin console because nothing was working. I am trying to find a way to get the value in there now, below is my code that I'm using.


var productAttributes = _productAttributeService.GetProductAttributeMappingsByProductId(productId);
            var attXml = productAttributes.Aggregate(string.Empty, (attributesXml, attribute) =>
            {
                var attributeValues = _productAttributeService.GetProductAttributeValues(attribute.Id);
                foreach (var selectedAttributeId in attributeValues
                    .Where(v => v.IsPreSelected)
                    .Select(v => v.Id)
                    .ToList())
                {
                    attributesXml = _productAttributeParser.AddProductAttribute(attributesXml,
                        attribute, selectedAttributeId.ToString());
                }
                return attributesXml;
            });
_productAttributeService.InsertProductAttributeValue(new ProductAttributeValue
            {
                AssociatedProductId = productId,
                AttributeValueType = AttributeValueType.Simple,
                ProductAttributeMapping = productAttributes,//suspicious this is the cause.
            });
6 years ago
In Case anyone is looking to do this I figured out how to do this.

under the IProductAttributeParser there is a method called AddProductAttribute where you can specify the attribute xml, product mapping, and the value. I was able to pass the data I needed through that.


here was the finishing result:



var productAttributes = _productAttributeService.GetProductAttributeMappingsByProductId(productId).ToList();

            var productMapping = productAttributes.Find(x => x.ProductAttribute.Name == "PageFlex Document Id");

            string attXml = "";

            attXml = _productAttributeParser.AddProductAttribute(attXml, productMapping, docId);


           _shoppingCartService.AddToCart(currentCustomer, currentProduct, ShoppingCartType.ShoppingCart, storeId, attXml, price, null, null, quantity);

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.