简体   繁体   中英

Dividing item.price in shopify

I want to divide {{ item.price | money_without_currency }} by 1.21. I am calling on the item.price in a hidden input value.

<input type="hidden" name="PRODUCT_PRICE_{{ forloop.index }}" value="{{ item.price |
money_without_currency }}">
When I do the below, it shows the value as "100/1.21" as my test product is 100. Makes sense since HTML doesn't do math.

<form name="addToFavorites" method="post" action="contoso.com">
<input type="hidden" name="PARTNER_KEY" value="34234234234234">
<input type="hidden" name="TOTAL_DOMESTIC_SHIPPING_CHARGE" value="0">
{% for item in cart.items %}
<input type="hidden" name="PRODUCT_ID_{{ forloop.index }}" value="{{ item.sku }}">
<input type="hidden" name="PRODUCT_NAME_{{ forloop.index }}" value="{{ item.title }}">
<input type="hidden" name="PRODUCT_PRICE_{{ forloop.index }}" value="({{ item.price |
money_without_currency }}/1.21)">
<input type="hidden" name="PRODUCT_Q_{{ forloop.index }}" value="{{ item.quantity }}">
<input type="hidden" name="PRODUCT_SHIPPING_{{ forloop.index }}" value="">
<input type="hidden" name="PRODUCT_CUSTOM_1_{{ forloop.index }}" value="{{ item.variant.title }}">
<input type="hidden" name="PRODUCT_CUSTOM_2_{{ forloop.index }}" value="">
<input type="hidden" name="PRODUCT_CUSTOM_3_{{ forloop.index }}" value="">
{% endfor %}
</form>

When I try to play with Javascript, it still just posts over the equation 100/1.21 and not the solution

<script>
var withoutTax = {{ item.price | money_without_currency }} / 1.21
var euPrice = "name=\"PRODUCT_PRICE_{{ forloop.index }}\" value=\"+withoutTax+\""

window.onload = function() {
       //when the document is finished loading, replace everything
       //between the <a ...> </a> tags with the value of splitText
   document.getElementById("euPrice").innerHTML=euPrice;
} 

</script>

<form name="addToFavorites" method="post" action="https://foo.com">;
<input type="hidden" name="PARTNER_KEY" value="987979879879879">
 <input type="hidden" name="TOTAL_DOMESTIC_SHIPPING_CHARGE" value="0">
  <input type="hidden" name="ORDER_CURRENCY" value="EUR">
{% for item in cart.items %}
<input type="hidden" name="PRODUCT_ID_{{ forloop.index }}" value="{{ item.sku }}">
<input type="hidden" name="PRODUCT_NAME_{{ forloop.index }}" value="{{ item.title }}">
<input type="hidden" id="euPrice">
<input type="hidden" name="PRODUCT_Q_{{ forloop.index }}" value="{{ item.quantity }}">
<input type="hidden" name="PRODUCT_SHIPPING_{{ forloop.index }}" value="">
<input type="hidden" name="PRODUCT_CUSTOM_1_{{ forloop.index }}" value="{{ item.variant.title }}">
<input type="hidden" name="PRODUCT_CUSTOM_2_{{ forloop.index }}" value="">
<input type="hidden" name="PRODUCT_CUSTOM_3_{{ forloop.index }}" value="">
{% endfor %}
</form>

My ultimate goal is to take a $100 product, discount it by 21%, and insert Please help by dividing the item price by 1.21 before the post the info.

Use Javascript parseInt

var priceasnumber = parseInt({{ item.price | money_without_currency }});
var withoutTax = priceasnumber / 1.21;

您可以使用divided_by数学过滤器:

{{ item.price | divided_by: 1.21 | money_without_currency }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM