Developer forum

Forum » Dynamicweb 10 » Add to cart from Api

Add to cart from Api

Jason
Reply

Hi, I have opened one thread regarding to this before. But I can't open it anymore and can't see your reply because I am seeing object reference null issue on that thread. So let open new thread again here.

I have created a cart with one product using this api.

After that I use the following api to see my cart and everything is correct.



after that , i add one more product into the cart using following api. But this time , when i check my cart back, my last order line is missing prices. Every prices are returning 0 and cart total is not changed as well.


Am I missing something or is there any issue?


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

This was the answer:

Please provide the payload you send to the post carts/item endpoint.

And a screenshot of your setup of that product in the backend. Do you have live integration?

Also refer to the docs: https://doc.dynamicweb.dev/documentation/headless/delivery-api/commerce.html#carts

 
Jason
Reply

We do have live integration but only after order is completed. My issue is happening for every products, not for specific product.


Here is the example payload

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Explain why you added orderlinetype and productnumber to the payload? Why both a productnumber and the productid? Why an orderlineType? I am asking because the docs has another example and I just want to understand what you are trying to accomplish with this combination?

Not sure this is the fix - but drop orderLineType from the AddLineToCart payload entirely for normal catalog products. Send only:

{
  "productId": "16018",
  "productVariantId": "",
  "productLanguageId": "LANG1",
  "quantity": 2
}
Let the engine assign the type and resolve the price from the catalog. (productNumber is also optional and only overrides the displayed number - if the product with the productid has a productnumber already, DW will add that it self.

The most likely other potential issues - all verifiable only against your actual data:

  • Assortment / user permission. The lookup now passes cart.CustomerAccessUserId (recently changed, commit 94c780f51d). If product 16018 (your second add) is restricted for this cart's user by assortment, GetProductById returns null → and since you send no unitPrice, the line is silently not created (NotFound). Worth checking the second call's HTTP status — was it actually 200?
  • Unit pricing. If 16018 has prices defined per a specific unit and your line's UnitId is empty, pricing resolves via DefaultUnitId fallback (PriceProductSelection.cs:66-76) — fine unless the product has no default unit / no unit-less price row.
  • No valid price row for that product at the cart's currency/customer-group/quantity — yields 0 legitimately.

To pin it down I need: Was the second product the same productId as the first, or different? What was the HTTP status of the second add? And is product 16018 unit-priced / variant / assortment-restricted?

Also disable live integration to test.

 

You must be logged in to post in the forum