1

I'm creating a web shop based on Sitecore Commerce 9. We are not using SXA. I'm trying to show cart contents on the page. My environment was populated with the example project from SDK.

The problem is that when I add an item to cart and then load the cart, the lines contain only mysterious ProductId, and no reference to neither SellableItem nor Sitecore item.

The problem is repoducible with Postman:

  1. Add item to cart:

Add item to cart

  1. Load the cart - see that all the line properties are empty:

Load the cart

I use the following class to load the cart programatically and I get same result - no reference to the item.

public class CartManager : ICartManager
{
    private CartServiceProvider _cartServiceProvider;
    private IStoreContext _storeContext;
    private IVisitorContext _visitorContext;

    public CartManager(IVisitorContext visitorContext, IStoreContext storefrontContext)
    {
        this._cartServiceProvider = (CartServiceProvider)Factory.CreateObject("cartServiceProvider", true);
        this._storeContext = storefrontContext;
        this._visitorContext = visitorContext;
    }

    public Cart GetCart()
    {
        var cart = _cartServiceProvider.LoadCart(new LoadCartRequest(this._storeContext.ShopName, "Default", this._visitorContext.CustomerId));
        if(cart.Success)
        {
            return cart.Cart;
        }
        return null;
    }

The only property filled is the Id (in Postman) or the line.Product.ProductId (in the code) which doesn't refer to anything.

How to get the SellableItem or the Sitecore item for the cart line?

Update: There is an error in the log:

    6 22:01:28 INFO Executing action method "Sitecore.Commerce.Plugin.Carts.CommandsController.AddCartLine (Sitecore.Commerce.Plugin.Carts)" with arguments (["System.Web.Http.OData.ODataActionParameters"]) - ModelState is Valid
6 22:01:28 ERROR Inventory.block.PopulateItemAvailability-No ProductArgument in Context
6 22:01:28 ERROR PopulateItemAvailabilityBlock.AllocationNull.|6042283|56042283
6 22:01:28 WARN PopulateItemAvailability.NotAvailable.|6042283|56042283
6 22:01:28 ERROR Pipeline completed with error
System.Exception: Error processing block: Preorderable.Block.PopulateItemPreorderableAvailability ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at Sitecore.Commerce.Plugin.Preorderable.PopulateItemPreorderableAvailabilityBlock.<Run>d__2.MoveNext()
   --- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Sitecore.Framework.Pipelines.ReflectionPipelineBlockRunner.<InvokeBlock>d__2.MoveNext()
   --- End of inner exception stack trace ---
   at Sitecore.Framework.Pipelines.ReflectionPipelineBlockRunner.<InvokeBlock>d__2.MoveNext()
   --- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Sitecore.Framework.Pipelines.BasePipelineBlockRunner.<Run>d__3`1.MoveNext()

And this is my IAddCartPipeline configuration:

{
        "Name": "IAddCartLinePipeline",
        "Policies": [],
        "Namespace": "Sitecore.Commerce.Plugin.Carts",
        "Receives": "Sitecore.Commerce.Plugin.Carts.CartLineArgument",
        "Returns": "Sitecore.Commerce.Plugin.Carts.Cart",
        "Blocks": [
            {
                "Name": "ValidateSellableItemBlock",
                "Policies": [],
                "Namespace": "Sitecore.Commerce.Plugin.Catalog",
                "Receives": "Sitecore.Commerce.Plugin.Carts.CartLineArgument",
                "Returns": "Sitecore.Commerce.Plugin.Carts.CartLineArgument"
            },
            {
                "Name": "AddCartLineBlock",
                "Policies": [],
                "Namespace": "Sitecore.Commerce.Plugin.Carts",
                "Receives": "Sitecore.Commerce.Plugin.Carts.CartLineArgument",
                "Returns": "Sitecore.Commerce.Plugin.Carts.Cart"
            },
            {
                "Name": "AddContactBlock",
                "Policies": [],
                "Namespace": "Sitecore.Commerce.Plugin.Carts",
                "Receives": "Sitecore.Commerce.Plugin.Carts.Cart",
                "Returns": "Sitecore.Commerce.Plugin.Carts.Cart"
            },
            {
                "Name": "ICalculateCartLinesPipeline",
                "Policies": [],
                "Namespace": "Sitecore.Commerce.Plugin.Carts",
                "Receives": "Sitecore.Commerce.Plugin.Carts.Cart",
                "Returns": "Sitecore.Commerce.Plugin.Carts.Cart"
            },
            {
                "Name": "ICalculateCartPipeline",
                "Policies": [],
                "Namespace": "Sitecore.Commerce.Plugin.Carts",
                "Receives": "Sitecore.Commerce.Plugin.Carts.Cart",
                "Returns": "Sitecore.Commerce.Plugin.Carts.Cart"
            },
            {
                "Name": "AddCartLineDigitalProductBlock",
                "Policies": [],
                "Namespace": "Sitecore.Commerce.Plugin.DigitalItems",
                "Receives": "Sitecore.Commerce.Plugin.Carts.Cart",
                "Returns": "Sitecore.Commerce.Plugin.Carts.Cart"
            },
            {
                "Name": "AddCartLineWarrantyBlock",
                "Policies": [],
                "Namespace": "Sitecore.Commerce.Plugin.DigitalItems",
                "Receives": "Sitecore.Commerce.Plugin.Carts.Cart",
                "Returns": "Sitecore.Commerce.Plugin.Carts.Cart"
            },
            {
                "Name": "AddCartLineInstallationBlock",
                "Policies": [],
                "Namespace": "Sitecore.Commerce.Plugin.DigitalItems",
                "Receives": "Sitecore.Commerce.Plugin.Carts.Cart",
                "Returns": "Sitecore.Commerce.Plugin.Carts.Cart"
            },
            {
                "Name": "AddCartLineGiftCardBlock",
                "Policies": [],
                "Namespace": "Sitecore.Commerce.Plugin.GiftCards",
                "Receives": "Sitecore.Commerce.Plugin.Carts.Cart",
                "Returns": "Sitecore.Commerce.Plugin.Carts.Cart"
            },
            {
                "Name": "PersistCartBlock",
                "Policies": [],
                "Namespace": "Sitecore.Commerce.Plugin.Carts",
                "Receives": "Sitecore.Commerce.Plugin.Carts.Cart",
                "Returns": "Sitecore.Commerce.Plugin.Carts.Cart"
            },
            {
                "Name": "WriteCartTotalsToContextBlock",
                "Policies": [],
                "Namespace": "Sitecore.Commerce.Plugin.Carts",
                "Receives": "Sitecore.Commerce.Plugin.Carts.Cart",
                "Returns": "Sitecore.Commerce.Plugin.Carts.Cart"
            }
        ],
        "Comment": ""
    }
4
  • No warnings or errors in the log? And can you post the pipeline configuration of the Sitecore.Commerce.Plugin.Carts.IAddCartLinePipeline using plumber or the Postman -> DevOps -> Get Registered Pipelines call?
    – Jonne Kats
    Commented Jul 19, 2018 at 15:19
  • Can you see at least quantity stored correctly for that cart line? Commented Jul 19, 2018 at 16:41
  • @PeterProchazka the quantity is stored and retrieved correctly. Commented Jul 19, 2018 at 19:50
  • @JonneKats Actually there is an error in the log, I updated the question both with the log and pipeline configuration. Commented Jul 19, 2018 at 20:08

1 Answer 1

1

The fix for this is problem is described in this post: https://sitecore.stackexchange.com/a/10592/3754

Basically, the Commerce Engine developed from SDK sample and stripped of the plugin samples is missing some pipelines, that are responsible for filling out the entities.

using Microsoft.Extensions.DependencyInjection;
using Sitecore.Framework.Pipelines.Definitions.Extensions;

// You may need to add references to these plugins
using Sitecore.Commerce.Plugin.Carts;
using Sitecore.Commerce.Plugin.Catalog;
using Sitecore.Commerce.Plugin.Coupons;
using Sitecore.Commerce.Plugin.Fulfillment;
using Sitecore.Commerce.Plugin.Payments;
using Sitecore.Commerce.Plugin.Promotions;
using Sitecore.Commerce.Plugin.Tax;

namespace Sitecore.Commerce.Engine
{
    public static class ServiceCollectionExtensions
    {
        public static IServiceCollection ConfigureCartPipelines(
            this IServiceCollection services)
        {
            services.Sitecore().Pipelines(config => config
                .ConfigurePipeline<ICalculateCartLinesPipeline>(builder => builder
                    .Add<PopulateCartLineItemsBlock>()
                    .Add<CalculateCartLinesPriceBlock>()
                    .Add<ValidateCartLinesPriceBlock>()
                    .Add<CalculateCartLinesSubTotalsBlock>()
                    .Add<CalculateCartLinesFulfillmentBlock>()
                    .Add<ValidateCartCouponsBlock>()
                    .Add<CalculateCartLinesPromotionsBlock>()
                    .Add<CalculateCartLinesTaxBlock>()
                    .Add<CalculateCartLinesTotalsBlock>(),
                    order: 1001
                )

                .ConfigurePipeline<ICalculateCartPipeline>(builder => builder
                    .Add<CalculateCartSubTotalsBlock>()
                    .Add<CalculateCartFulfillmentBlock>()
                    .Add<CalculateCartPromotionsBlock>()
                    .Add<CalculateCartTaxBlock>()
                    .Add<CalculateCartTotalsBlock>()
                    .Add<CalculateCartPaymentsBlock>()
                )

                .ConfigurePipeline<IAddPaymentsPipeline>(builder => builder
                    .Add<ValidateCartHasFulfillmentBlock>()
                        .After<ValidateCartAndPaymentsBlock>()
                )
            );

            return services;
        }
    }
}

Which you can then call in your ConfigureServices class:

services.ConfigureCartPipelines();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.