How to Add Multiple Items to a Shopping Cart via REST in a Single Call?

Hi,

We are currently using the REST process n_webshoppingcart_no_user_add_to_webshoppingcartcontrollercomponent to add products to an existing shopping cart, this works without any issues. However, we are only able to add one product at a time with this method. This is good for the normal website visitor.

But in case for upselling, is there a way to send an array of products in a single REST call so that multiple items can be added to the shopping cart at once? This would allow us to optimize the process by reducing the number of calls needed. Any advice would be very helpfull.

Hey Stephan,

The technical question is clear.

However, I am curious what the exact use case is, just to make sure that we solve the right problem. There is existing functionality to add additional products under certain conditions automatically. For example, if the user choses product A, he will always get product B for free. Then only product A has to be passed via the endpoint, and the logic for adding extra products stays in the backend. “M5328 - Novulo Kortingscodes” covers a lot of use cases for special deals, limited offers, vouchers, extra products, etc.
Let me know if you are interested.

However, if you are sure that you want to add multiple items at once, follow the steps below:
This would probably require a new process. For example, in M10452 or in a new custom component. The difficulty lies in the input parameter for passing the new shopping cart items.
One item consists of three properties:

  1. Sales price list item
  2. Quantity
  3. Force new line

You can use a parameter “items” of type string to pass a JSON of all the items to your process. The JSON to pass the items would need to look like this.

{
  "items": [
    {
      "item": {
        "spli": "123",
        "qty": 2,
        "force_new_line": true
      },
      {
      "item": {
        "spli": "456",
        "qty": 1,
        "force_new_line": false
      },
      ...
    }
  ]
}

Then, in your new process, you can convert the JSON into a “list<tuple <String,Mixed>>” with a DeserializeObjectV2 action. You will need the serialization plugin for that.
Then, the tuple list can be processed and the data of the individual items can be extracted with “TupleExtLookup” actions. You will need the tuple extension plugin for that. When all data is extracted, you can use it for the logic of the process that will be similar to “N_WebShoppingcart - No user - Add to WebShoppingcart” in M10452.

Disadvantages: You will hardcode the keys for the values in an “item” object. So “spli”, “qty” etc can only be changed through an application update.

If you have questions on how to use the plugin action, I will probably create a separate and more generic wiki post per plugin.

1 Like

Hi Joachim,
This is exactly what we are looking for. Since we need to develop the component for this, we will address it for the customer in a later phase. Thank you for the example and clear explanation.

1 Like