Let's say, you've added product A to your shopping cart. Then you get back to the product details page and add product A again. What's going to happen? You will still have one line in your cart, but the quantity of product A will be equal two. This is a standard behavior of B2C shopping carts, including the Sitecore Commerce 9 ones.
Now, imagine that your business processes require a bit more complicated scenarios, where you might want to actually create two lines in the cart, and avoid rolling them up into a single one. It is much more common than you might think. For example, you are buying two toasters of the same make and model, but you want only one of them gift-wrapped. Or you buy two pairs of glasses belonging to the same SKU, but you want to specify different prescriptions for them. Obviously, you do not want to roll them up under a single cart line.
What does Sitecore Experience Commerce 9 provide you with in order to handle such scenarios? Let's take a look at the developer documentation. The Commerce Connect documentation page for Cart Service Provider has the following:
Now, imagine that your business processes require a bit more complicated scenarios, where you might want to actually create two lines in the cart, and avoid rolling them up into a single one. It is much more common than you might think. For example, you are buying two toasters of the same make and model, but you want only one of them gift-wrapped. Or you buy two pairs of glasses belonging to the same SKU, but you want to specify different prescriptions for them. Obviously, you do not want to roll them up under a single cart line.
What does Sitecore Experience Commerce 9 provide you with in order to handle such scenarios? Let's take a look at the developer documentation. The Commerce Connect documentation page for Cart Service Provider has the following:
Excellent! Sitecore's got you covered! You can use ForceNewLines attribute to control rolling up the cart lines at any time, on demand. So, you quickly implement and try using it with your standard Sitecore Experience Commerce 9 installation. What happened? Why do you still have a single cart line even with ForceNewLines=true? That's when you notice that little note in documentation, saying "This parameter is only supported if the external commerce system supports this functionality". Oh, c'mon, we're using Sitecore Commerce Engine - the most compatible system when it comes to Commerce Connect. It surely supports everything... Or is it?
OK, time to do the most common thing in your daily Sitecore Commerce developer routine: decompile Sitecore code. Even a quick look at the Sitecore.Commerce.Plugin.Carts.AddCartLineBlock reveals that none of its input CartLineArgument parameters are being used to control the lines rollup behavior. None. Great.
Frustrated, you look through the code again, and notice the following:
What is that? It actually is one of the Cart Policies, that can be located in the standard environment policy files. Just set it like this, and you're covered:
Well, not exactly. You just switched your system from one mode to another. In order to control it on demand, you would need to write a pipeline block that recognizes the desired mode and dynamically sets RollupCartLinesPolicy in the CommerceContext. Placing it in front of the AddCartLineBlock would make the Commerce Engine great again a bit more flexible.
Comments
Post a Comment