2

I add items to my cart using ajax, following the code from this post

Update Commerce Cart using Ajax

But there are some things missing in the cart object returned on success, they are :

  • The URL of the page of the product added
  • The images, and all the fields from the product added
  • The name of the variant of the product added

Are these somewhere in the object returned ? Otherwise, how could I retrieve them ?

1

As Brad said in his answer, what you get back is the lineItem details - enough to update e.g. your totals, shipping quote etc.

The URL of the product, and the product fields etc, are not available in the JSON. But it's hard to see why they would need to be - you're sending this ajax call from somewhere you already have a reference to the product/variant in your twig templates - why would you need this info repeated in the return object?

If you really need that stuff at that point, you're probably better off ajax loading in a twig template to get at the full product stuff. If you could clraify what you're actually trying to do I can probably help better/describe what we've done.

  • Jeremy, can you elaborate on how you would AJAX load a twig template? I'm kind of stuck myself trying to AJAX add to cart and get a product thumbnail in return. – John Johnson Jun 8 '18 at 20:34
  • It's not hard really...just some JS to load a template, like this, where $cart is e.g. a reference to a div to hold the cart: ` $cart.load("/ajax-injects/cart", function() { $cart.fadeIn(400); }); }); ` Then in that loaded template get the cart and loop through it like you normally would, getting the product (or variant) from the line item, and the image from the product (or variant) `{% set cart = craft.commerce.cart %} etc If that's not enough post another question and point me at it here or in Slack and I'll do a proper answer :) – Jeremy Daalder Jun 11 '18 at 6:12
  • First, I should say I had trouble even loading a template with plain text in it simply because it was in a folder starting with an underscore (_partials/). That was my first mistake. Secondly, after adding the twig to the template, it no longer loads. Is twig able to render in a jquery loaded template? – John Johnson Jun 13 '18 at 15:30
  • Ok, NVM. Had a typo in my twig code, which probably drew a template error that wasn't showing via jquery .load. I'll carry on in this direction and report back. Thank you, Jeremy! – John Johnson Jun 13 '18 at 15:50
  • No worries. Use the network thing in your broswer's dev tools and check the response - you'll see twig errors in there if you have them. But you can do basically anything in an ajax loaded template you can in a normal one, it's jsut a bit fiddlier when you make a woopsie ;) – Jeremy Daalder Jun 13 '18 at 23:20
0

A Commerce_OrderModel is returned in the response of a successful call to Commerce_CartController->actionUpdateCart().

The product info you're looking for, you'd get through the lineItems property, which would be an array of Commerce_LineItemModels.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.