skip to Main Content

Please help me to resolve this code. I have some % off discount to show based on the variant choose, its not getting changed on clicking on pack of 2 and pack of 3, as we have different % in discounts. This is my shopify product url https://us.buywow.com/products/wow-apple-cider-shampoo-cocounut-oil-conditioner-set-500ml
Callback function is written, but I am not understanding why its not changing

Product.liquid

<!-- /templates/product.liquid -->
{% assign on_sale = false %}
{% if product.compare_at_price > product.price %}
{% assign on_sale = true %}
{% endif %}

.

.

.

.

{% comment %}
------ Product Price ------
{% endcomment %}
<div class="product_prices">
<span class="visually-hidden">{{ 'products.general.regular_price' | t }}</span>
<h4 id="ProductPrice" class="product-regular-price" itemprop="price" content="{{ current_variant.price | divided_by: 100.00 }}">
{{ current_variant.price | money }}
</h4>
<span class="old-price">{{ current_variant.compare_at_price | money }}</span>
{% if on_sale %}
<div class="save_money_block">
<span class="save_off">
{% if current_variant.compare_at_price > current_variant.price %}
{{ current_variant.compare_at_price | minus: current_variant.price | times: 100.0 |
divided_by: current_variant.compare_at_price | money_without_currency | times: 100
| replace: '.0', ''}}% OFF{% endif %}
</span>
</div>
{% endif %}
</div>

.

.

.

.

.

<script>
(function(s3d) {
if (!s3d) {
console.warn('"window.Shopify3d" does not exist. Please ensure you've added the <script> to your theme');
return;
}
{% for variant in product.variants %}
s3d.mapMetafieldAssets('{{ variant.id }}', '{{ variant.metafields.shopify3d['assets'] }}');
{% endfor %}
})(window.Shopify3d);
</script>

<script>
var selectCallback = function(variant, selector) {
timber.productPage({
money_format: "{{ shop.money_format }}",
variant: variant,
selector: selector
});
};

</script>

On selecting Pack Of 3 – 60% OFF should be the resultant.
that is not happening auto refresh.

2

Answers


  1. Remember that Liquid is compiled server-side before the document is sent to the client’s browser, so the result is not dynamic and will not be able to automatically update when a variant is selected.

    In order to make the percent-off part change appropriately when the selections change, you will need to add javascript code that will run whenever the variant selection changes. Fortunately, you already have a function that will do that, in your theme it happens to be called selectCallback, which then calls a function called timber.productPage to do all the heavy lifting.

    You will want to update either selectCallback or timber.productPage with the javascript code needed to update your percent-off field. (The latter is probably the better option in your case, as that would keep all of the relevant price-display-updating code close together.) The variant parameter will either be the currently-selected variant or undefined if the current option selections do not match any of the variants in the product; the selector parameter will contain a lot of generally-useful information, such as the parent product object.

    Hope this helps!

    Login or Signup to reply.
  2. In the selectCallback function add next code:

    <script>
    var selectCallback = function(variant, selector) {
    var $discount = $('#offpercent'),
        $productPrice = $('#productPrice'),
        $comparePrice = $('#comparePrice');
    
    alert(Shopify.formatMoney(variant.price, Shopify.money_format));
    alert(Shopify.formatMoney(variant.compare_at_price, Shopify.money_format));
    
    var percDiff =  Math.round((variant.compare_at_price-variant.price)/variant.compare_at_price * 100);
    
    alert(percDiff);
    
    $discount.html(percDiff);
    };
    </script>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search