Home > front end >  Spartacus add to cart
Spartacus add to cart

Time:02-24

With Spartacus I have been able to change the PDP call to take an extra parameter which is the store code and its working fine. But when I add the product to the cart the api call fails because the store code is not being passed through.

I have started by extending the active-cart and multi-cart services to take my extra parameter and I am struggling with one of the private variables that are used in the function that are not exposed.

Am I on the right path or is there an easier way to add a parameter to this call? Is an intercepter going to work?

CodePudding user response:

This is the easy way for you:

1 - Create a service that will provide the store code (maybe you already have it)

2 - Override original configuration for Occ addEntries endpoint to use your additional parameter (e.g., addEntries: 'users/${userId}/carts/${cartId}/entries?storeId=${storeId}' )

3 - Extend OccCartEntryAdapter from Spartacus, inject the service from Step 1 in the constructor, and override add method; the method body will be almost equal as the original Spartacus code, with the additional that you will get toe storeId from the service injected, and you will add it to buildUrl call:

this.occEndpointsService.buildUrl('addEntries', {
  urlParams: { userId, cartId, storeId },
});

Please notice that a slight difference might be if you defined the value to be send inside the body, but you will easily figure it out: It won't be on urlParams, but in the body (variable toAdd).

Please let me know. Thanks!

  • Related