When displaying the cart contents, the cart object needs to come directly from the JSON object. Currently, it's being fetched and passed to the DOM and back. As a result, special character encodings are an issue. In particular, single quotes break the cart functionality.

Comments

lisastreeter created an issue. See original summary.

lisastreeter’s picture

Issue summary: View changes
lisastreeter’s picture

StatusFileSize
new3.37 KB

Patch updates the commerce-cart-flyout-offcanvas-contents.html.twig template so that it only prints the order id, not the entire JSON object string. CartContentsItemsView uses the order id to get the cart JSON object from the model.

lisastreeter’s picture

Status: Active » Needs review
mglaman’s picture

I wish we could just use Array.prototype.find, but IE11 does not support it (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global...). So this is probably our best approach.

mglaman’s picture

+++ b/templates/commerce-cart-flyout-offcanvas-contents.html.twig
@@ -1,3 +1,3 @@
-    <div data-cart-contents='<% print(JSON.stringify(cart)) %>'></div>
+    <div data-cart-contents='<%= cart.order_id %>'></div>

We do have one problem: anyone who has customized that template will have a breaking change.

Example: http://cgit.drupalcode.org/belgrade/tree/templates/commerce/cart/commerc...

Which, I don't know why that file was overridden.

lisastreeter’s picture

We do have one problem: anyone who has customized that template will have a breaking change.

Yep! Realized that this morning when I applied the patch to a local site and forgot it had the template in its custom theme.

Maybe a slight improvement would be to create a template with a new name to replace commerce-cart-flyout-offcanvas-contents.html.twig
Existing theme overrides would then simply be ignored. But at least the js wouldn't be broken because of a template with the (now) wrong structure...

mglaman’s picture

StatusFileSize
new5.72 KB

I was thinking this over, and I think we can avoid the breaking change, this way. We assume the cart form elements will be added in the same sequence as the array. That means we can pass the matching cart via a key to the next view.

lisastreeter’s picture

Status: Needs review » Reviewed & tested by the community

This newest patch is working for me. Products with quotes in their names can be added to the cart without breaking the cart functionality.

  • mglaman committed acb16b6 on 8.x-1.x
    Issue #3011068 by lisastreeter, mglaman: Order items with quotes in...
mglaman’s picture

Status: Reviewed & tested by the community » Fixed

Fixed!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.