Skip to main content

Sitecore commerce multiple AddToCart buttons

Hello Fellow Developers,

Today i will be sharing some information about sitecore commerce component called "AddToCart", One of the basic requirement of any e-commerce site is to have an AddToCart button on any component of your choice which deals with the product you are selling.

We have had similar requirement where just like ProductList or PromotedProduct component, We were supposed to create similar component called "ProductParts" component, What it supposed to do was to show the different sellable part of the product you are visiting.

So for example if you are selling a mobile so on any specific product detail page of that mobile, there should also be one component which could show different parts or accessories which user can also buy and perform AddToCart from there itself.

One thing we observed was that existing ProductList and PromotedProducts etc. none of them are having AddToCart button on them, So visitors can see those items but to add item to cart, they have to visit the main product detail page (PDP) and add the item to cart from there

Problem

We are resolving two issues here

1) Have an AddToCart button rendering or a control which can appear on any page, Existing AddToCart rendering when you drop it on the page, it reads item from the main Product item but if i have other items for which i want to AddToCart for, It is not possible, Until i go to the main PDP and add that item to cart.

2) Existing AddToCart works on Product item only, if you have a requirement where all of your data attributes of your sellable entities are residing on a variant, OOTB button will not work

So, if you have been working with sitecore commerce, You must have observed that the whole infrastructure of sitecore commerce is based on a sellable product and not it's variant, meaning if you are selling one mobile with 5 different variant, commerce's pipeline and their repository only works on the main product, so the resolving item is always a product and not any variant, so context item will always be a product.

Similarly if you observe the AddToCart button it only reads the fields from context item we are on, so for instance if you are on "Apple Iphone 5s" mobile and now if you want use AddToCart button for the ProductPart component which is "Iphone 5s cover", it is not possible with existing button because it reads data only from context item and context item is IPhone 5 and not "Iphone 5s cover"

If you see the implementation of  AddToCart.cshtml (i am putting screenshot only for the reference of the code i want to show) 


If you see above, the AddToCart button can only add products and because it passes variantId as empty, that means even if you have managed to resolve Variant as you rendering item, when you do AddToCart, It will add the main product to cart only

Also there is no way we can add existing AddToCart on our custom component "ProductPart"

Now because of this, we decide to create our own AddToCart button which will follow the same pattern and hidden fields and same mark up as main AddToCart button, but to get the data of the variant it is on (Iphone 5s Cover), We will read this item from commerce get require data and set those hidden fields on this new AddToCart button.

Solution

To get all of these working, We created our own repository, model and cshtml, cshtml was exact copy of existing AddToCart.cshtml which ships with OOTB commerce component, Following is a summary

1) We created one respository which is basically reading the data from commerce for our variants, and because "ProductPart" component is responsible to show parts of the current product, we have had one field in commerce for every product which will have parts field populated (in biztool), so our repository can read those parts, so we can show them in our custom ProductParts view


As seen in the screenshot above, ProductPart is our model for "ProductPart" component which has one property called "CartModel" which is nothing but our custom AddToCartRenderingModel (almost the copy of existing OOTB model), and we are filling that model with required properties by reading from commerce and giving it reference to "VariantId"

2) We create our custom AddToCart.cshtml, and our custom AddToCartRenderingModel.cs so that from our repository we could easily fill this model and we can populate all those properties which existing AddToCartRenderingModel.cs is not filling for example VariantId and simply in "ProductPart.cshtml" we can render the AddToCart button by rendering our custom AddToCart.cshtml partial view like following


 End result

So after reading all parts of the product in product repository and filling AddToCartRenderingModel for all of those parts in the repository, end result looks like following, where all the parts for current product can be seen with AddToCart button in it

 

Let me know if this helps, There are so many customization you can do with Sitecore commerce, as they say "Requirement is mother of all inventions", Happy customizing Sitecore commerce !!!

Comments

Post a Comment

Popular posts from this blog

Sitecore Technical Workshops - Top FAQs customers asked on XM Cloud

Hi Readers, I want to talk to you about interesting things which we have been doing which is "Technical Workshops" for our customers, so here goes the scenarios. So, we have been doing multiple types of technical workshops.  1) Training customer and their Sitecore technical team on latest and the greatest technologies like XM Cloud & Another composable stack and try enabling them for new Sitecore tech stack. 2) Customers / Potential Customers have their agenda of existing pain points, and we take a workshop on topics around them with best practices etc. little on new technologies, so they also know the future. Basically, we prepare custom targeted presentations & demos for individual workshops, and make sure it helps them answer their questions and they get insights of where Sitecore eco systems has to offer from their versatile toolset and try to keep them up to date with it. So, Purpose of this blog is, because in all these customer & their technical team's

An error occurred while receiving the HTTP response to This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.

You have noticed many times that everything was working fine and suddenly the below error starts coming and you find no way to work it out An error occurred while receiving the HTTP response to This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details. The reason for this is the receiving size of WCF service is smaller then the data which is coming from service It was working before because it was small,So you will have to try to increase the receiving setting in your end point,Possible settings can be following maxStringContentLength="2147483647" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" maxArrayLength="2147483647" That would definately help you!!!

Set up leprechaun code generation with Sitecore XM Cloud Starterkit

Hi Sitecorians, It has been amazing learning year so far and with the change in technology and shift of the focus on frontend frameworks and composable products, it has been market demand to keep learning and exploring new things. Reasons behind this blog Today's topic is something that was in my draft from April-May, and I always thought that there is already a good documentation out there for  Leprechaun  and a blog post is not needed, Until I realized that there was so many of us facing same kind of issues and same kind of problems and spending same amount of time, That is where I thought, if I could write something which can reduce that repetitive troubleshooting time, That would really help the community. 1)  In a project environment, if we get into some configuration issues, we resolve them, we make sure we are not blocked and continue, but if you think same issue, same step and same scenario will come to other people, so if we can draft it online, it will help other people 2