Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTML / unicode encoding issue with line item properties #1696

Open
twilson90 opened this issue Mar 7, 2023 · 5 comments
Open

HTML / unicode encoding issue with line item properties #1696

twilson90 opened this issue Mar 7, 2023 · 5 comments

Comments

@twilson90
Copy link

twilson90 commented Mar 7, 2023

My products have color customization properties. The properties are stored in a product metafield as a json:

[
  {
    "name": "Branches & Birds Colour",
    "required": true,
    "type": "color",
    "colors": [
      {
        "name": "Bright Yellow",
        "value": "#f8dc0d"
      },
      {
        "name": "Light Orange",
        "value": "#f8971f"
      },
      {
        "name": "Orange",
        "value": "#f14a27"
      },
      ...
]

On the product page, the options are rendered as inputs with name="properties[Branches & Birds Colour]"

After adding an item to cart with properties, on the cart page if I print {{ cart.items.first.properties | json }} I get this:

[["Branches \u0026amp; Birds Colour","Leaf Green"],...]

The ampersand is converted into & then the & is unicode encoded... This appears to be the way all properties are encoded in the line item.
The equivalent of {{ string | escape | json }}.

But if I print {{ cart.items.first.product.metafields.namespace.custom_properties | json }} I get:

[{"name": "Branches \u0026 Birds Colour", ... }]

A problem then arises when I want to match the key in the cart to the metafield json property.

Practically, I want to show the color of each customization in the cart (using the hexadecimal value) by matching the property key with the metafield custom property definition, but the encoding messes this up.

I can't use url_decode on the line item property key, the unicode would need to be decoded first, and there appears to be no way in liquid to decode unicode.

So I'm stuck... unless I implement some hacky solution where I replace all instances of '\u0026amp;' with '\u0026', but this would only handle ampersand characters...

Is this a bug or am I missing something? Or is this just one more of Shopify's delightful quirks that I must learn to live with?
Either way, I'm welcome to any ideas on how to overcome this.

@twilson90
Copy link
Author

Just found another oddity.
If I add to cart an item with properties:
{ "<>":"something" }

In /cart.js the greater than and less than symbols are converted to unicode:
"\u003c\u003e":"something"

However in liquid, if I ever wanted to show the key of the property:

for prop in cart.items[0].properties
    echo prop[0]
endfor

Nothing is printed. Internally it appears that whenever I access a line_item property key (or value), anything resembling an html tag is stripped before it's given to me / printed.

And unless I'm mistaken this only appears to be with line_item properties. I can't recreate this behaviour elsewhere.

@yosef-grant
Copy link

yosef-grant commented Nov 2, 2023

@hedgehog90 im curious if you found a solution to the unicode problem - im having the same issue with strings containing single quotes, theyre being converted automatically to & # 3 9 ; and there seems to be no way of filtering this natively in Shopify.

@twilson90
Copy link
Author

Only solution I think is to embed the json in a script tag of type "application/json" and process it client-side with Javascript.

@luigisaggese
Copy link

@twilson90 did you found any solution?

@twilson90
Copy link
Author

Nope, see above ^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@luigisaggese @twilson90 @yosef-grant and others