ethermal
ethermal

Reputation: 358

Access the custom fields of the product from a SKUInfo object within ShoppingCart

We have custom fields and non-custom fields on a product page and thousands of products (approx. 250,000+ products). Information about a product contains things like warehouse and just attribute type information. When I look at an item in the shopping cart, I cannot access any of those custom fields.

I have tried the obvious ShoppingCart.CartItems[0].GetValue("Warehouse") which returns null. I also tried ShoppingCart.CartItems[0].SKU.GetValue("Warehouse") which also returns null.

When I go to the Pages application and then to the product page, then to the form tab I can see all the SKU fields and my custom fields displayed.

Upvotes: 1

Views: 364

Answers (2)

Enn
Enn

Reputation: 2209

The custom field for

ShoppingCartItemInfo

are not available because they are stored in a different (coupled) table. For example you can have different types of products (mobile, laptop, keyboard...) in your shopping cart and the custom field are not loaded by default.

You can use following code to get the custom fields:

        string culture = "en-US";
        var tree = new TreeProvider(MembershipContext.AuthenticatedUser);

        foreach (var shoppingCartItem in ECommerceContext.CurrentShoppingCart.CartItems)
        {
            // we need to get class name of item in shopping cart
            var productWithClassName = tree.SelectNodes()
                .Where("NodeSKUID", QueryOperator.Equals, shoppingCartItem.SKUID)
                .And()
                .Where("DocumentCulture", QueryOperator.Equals, culture)
                .Take(1)
                .FirstOrDefault();

            if (productWithClassName != null)
            {
                var product = tree.SelectSingleNode(productWithClassName.NodeID, culture);

                if (product != null)
                {                        
                    var customField = product.GetStringValue("CustomField", "-");
                }
            }
        }

As you can see it is a little more complicated because in order for you to access custom field you need to get the node associated with the product and then you need to get coupled data of this node.

On a sidenote - Storing 250k documents in Kentico 8 is not recommended because there are actually some limitations of how large the content tree can be. As a rule of thumb it is recommended to have 100k documents in Kentico 8 + maximum of 1 000 direct child documents under a single node.

It is very likely that you might encounter performance issues with such a large base of products. If you can't restructuralize the content (= custom classes or custom tables) then be sure to use caching very aggresively.

Upvotes: 1

Dawid Jachnik
Dawid Jachnik

Reputation: 447

Where you wanna use it ?

If in static text or in the K# this work for me:

EcommerceContext.CurrentShoppingCart.ShoppingCartItems[0].SKU.CustomFieldName

In the c# in CMSCheckoutWebPart similar function to your works:

ShoppingCart.CartItems.FirstOrDefault().SKU.GetValue<string>("CustomFieldName","")

Upvotes: 0

Related Questions