skip to Main Content

The goal is to check for an item in the shopping cart, then if it exists, reassign the item.product.id value another product ID.

The assign does not work in my code below. The output is equal to the original value.

{% for item in cart.items %}

{% if item.product.id == 4456879030362 %}
    {% assign item.product.id = 3962085671002 %}
    <div class="upsell-pop" style="text-align:center; width: 100%; border: 1px solid red;">
      <h4>Frequently bought together</h4>
        <p><a href="{{item.product.url}}">{{ item.product.title }}</a></p>
        <img src="{{ item.image.src | product_img_url: 'medium' }}">
        <span class="h3 price text-center">
          {% if item.original_price > item.final_price %}
            <s>{{ item.original_price | money }}</s>
          {% endif %}
          {{ item.final_price | money }}
        </span>
        <form action="/cart/add" data-productid="{{item.product.id}}"  method="post"> 
            <input type="hidden" name="id" data-productid="{{item.product.id}}" class="product-select" value="{{ item.product.variants[0].id }}" data-variant-title="{{ item.product.variants[0].title }}" />
            <input type="submit" value="Add To Cart" />
        </form>
    </div>
{% endif %}

{% endfor %}

2

Answers


  1. I’m not sure if you are trying to update the value via liquid ( which is not possible ) or you are using the form ( which wont work the way you are describing ).


    1) You can reassign objects

    This is not a valid code {% assign item.product.id = 3962085671002 %}.

    Assign is used for creating or overwriting variables. You can’t use it to modify objects and their values.

    2) When you are updating the cart you must do so via JS.

    If you wan’t to swap a specific product from the cart you must use the /cart/update.js end, see the docs for reference: https://help.shopify.com/en/themes/development/getting-started/using-ajax-api#update-cart

    3) When you are adding products to the cart you must use their variant.id.

    All products in Shopify use variants ( even if they don’t have one, their have a default hidden one ) that you buy. So you must pass the variant.id and not the product.id.

    So when you want to swap a specific product using the cart/update.js you must target the variant.id.


    So what should you do?

    You can still check the {% if item.product.id == 4456879030362 %} but at that point you have a few options.

    1) Add a specific class to the item and target that class with javascrip in order to swap the item with the new one from an external javascript file.

    2) Add the JS code directly in the IF statement and swap the product with /cart/update.js once again.

    {% for item in cart.items %}
        {% if item.product.id == 4456879030362 %}
            <script>
                jQuery.post('/cart/update.js',
                "updates[{{item.id}}]=0&updates[{{YOUR_NEW_VARIANT_ID}}]=1"
                );
            </script>
    
            ...
        {% endif %}
    {% endfor %}
    
    Login or Signup to reply.
  2. If I understand well what you try to achieve is to display an upsell add to cart form if a specific product is in cart.

    First, as drip was explaining, you cannot reassign an existing object attribute value recorded in database. You can only create new vars using the assign tag.

    But the good news is that you don’t need to to do what you want to do.

    Then, in Shopify, the key for an object is the handle attribute and not the ID attribute. For example, to get a specific product you can do it via its handle:

    {{ all_products['my-product-handle'].title }}
    

    You must also take in account that you need to add quotes when you us a string in a condition like this:

    {% if product.handle == 'foo' %}
    

    So, to achieve your goal, you could try this:

    {% for item in cart.items %}
      {% if item.product.handle == 'my-upsell-trigger-product-handle' %}
       {% assign upsell_product = all_products['my-upsell-product-handle'] %}
         {{ upsell_product.title }}
         {% form "product", upsell_product %}
            <input type="hidden" name="id" value="{{ upsell_product.selected_or_first_available_variant.id }}">
            <input type="submit" value="Add to cart" />
          {% endform %}
      {% endif %}
    {% endfor %}
    

    Not tested but this should work!

    HTH

    Useful documentation:

    Handles: https://help.shopify.com/en/themes/liquid/basics/handle

    Basic operators: https://help.shopify.com/en/themes/liquid/basics/operators#basic-operators

    Global objects: https://help.shopify.com/en/themes/liquid/objects#global-objects

    Add to cart form: https://help.shopify.com/en/themes/development/templates/product-liquid#build-the-html-form

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search