Subtotal of properties in notifications on a line-item level.

Highlighted
Shopify Partner
4 0 3

Hi all!

For a client I need to add in a second (static) tax. Let's say this tax is 1 dollar for each item.

Got this working by leveraging the line_item.properties, the barcode field (this is where the static tax is in the back-end) and a bit of JavaScript. It live updates in the cart, on the product page, checkout etc. So on the shop itself everything is working.

But we also need it in our order confirmation and invoices (order printer). That's both liquid. And that's where I'm stuck currently.

Let's say the car looks like this and I need the "8" that is stated in bold in the right bottom corner:

ProductPriceAmountSubtotalTaxExtra taxExtra tax total
Watermelon pistol52100.3012
Banana bike2061201916
       
TOTALS25813019.3028


My current code (more or less, this ain't the actual code):

 

{% assign amount = 0 %} -- Counts the total amount of products, works.
{% assign tax = 0 %} -- This is still buggy.
{% assign total-pant = 0 %}   
{% for line in subtotal_line_items %}
       {% if line.quantity < line.quantity %}
            {% capture line_display %} {{ line.quantity }} of {{ line.quantity }} {% endcapture %}
       {% else %}
            {% assign line_display = line.quantity %}
       {% endif %}

       {% capture temp %}{{ amount | plus:line.quantity }}{% endcapture %} 
       {% assign amount = temp %}

       {% for property in line.properties %}
             {% capture temp2 %}{{ tax | plus:property.last | times:line.quantity }}{% endcapture %}
             {% assign tax = temp2 %} --> Still gives unwanted results.
        {% endfor %}
{% endfor %}

 


For the cart and the checkout etc I needed to do the same, but I was able to use jQuery there. And that code looks like this:

 

  totalPant = function(selector) {
    var sum = 0;
    var current = 1;
    $(selector).each(function() {
      sum += Number($(this).text().match(/\d+/) * $('.cart__qty-input').eq(current).val());
      current += 1;
      console.log("fired outside")
    });
    return sum;
  }

	var pantSubtotal = totalPant('[data-cart-item-property-value]');
  
	$('#pant-sub').html("<p>Pant " + pantSubtotal/100 + " KR.</p>");

 

 
I'm probably missing something quite obvious here, but I'm not seeing it I guess. And by using the line_item.properties, I'm required to send myself a mail on every single change to test it. Any help would be appreciated!

0 Likes
Highlighted
Shopify Staff
Shopify Staff
1041 140 166

Hey @Datbaas,

To confirm requirements: you need to access line item properties and barcode value on order confirmation and invoices (order printer)?

Can you provide an example of the values in each? Is the barcode or line item property extra tax?

1 Like
Highlighted
Shopify Partner
2294 116 353

Before the cart goes goes into checkout set a cart attribute to carry the value through https://shopify.dev/docs/themes/liquid/reference/objects/cart#cart-attributes

  <input id="attributes_static_tax" type="text" name="attributes[static_tax]" value="{{ static_tax }}" enabled>

Or use ajax api. 

Then reference it in order templates

 

{{ attributes.static_tax }}

 

 

Then there's also an entry in the orders admin in additional details

Problem Solved? ✔️Accept and ? Like the solution so you can help others.
Buy me a coffee ☕ paypal.me/paulnewton or donate to eff.org
Confused? Busy? Buy a custom solution paull.newton+shopifyforum@gmail.com
1 Like
Highlighted
Shopify Partner
4 0 3

Hi Paul and Scotty!

Thanks for your replies. Solved it when I was able to look at the code with fresh eyes.

@PaulNewton I think that's kinda what I did with the line_item.property. Wasn't aware of the cart.attribute. Could you elaborate what the difference is here? Line_item is on product level and the cart on a cart level? Both seem to end up doing the same (didn't test, just an assumption). 

This would still leave me with same result. The tax per product instead of a "added tax" subtotal. Still thank you for your time, thinking along and pointing out the cart.attribute.

@SBD_ Yes currently using a product with variants. Customers can buy 2, 4 or 6 times the same product. And I used the barcode field to add 200,400 and 600 cents. This field auto populates the "line_item.property". In both the cart and checkout these are then calculated into a total added tax.


The solution (please keep in mind that each product should have 1 dollar/euro extra tax):
{% capture tax-line %}{{ line.quantity | times:property.last }}{% endcapture %} --> To calculate it on a line_item level. So 5 x the 2 product variant calculates to 10.
{% capture final-tax %}{{ added-tax | plus: tax-line }}{% endcapture %} --> This runs within the for loop causing to add up each added tax per line item.

Or at least something like that :). Got it working. Thanks for your time and ideas!

0 Likes
Highlighted
Shopify Partner
2294 116 353

Cart attributes show as additional information in the sidebar in the shopify orders admin, LIP's show as info under products in the shopify order admin.

Recommend to make a test order with the different types and see then type of end result.

 

Taking another look at the code by "amount" you mean item_count?

Just in case: if the tax is the same for all items use {{ cart.item_count | times: 1.5 }} 

and there's a typo:

 {% if line.quantity < line.quantity %}

Also is the capture tag being used because the assign tag creates errors when adding the amount and line.quantity ?

       {% capture temp %}{{ amount | plus:line.quantity }}{% endcapture %} 

Can be {% assign temp = amount |plus: line.quantity %}


@Datbaas wrote:

This would still leave me with same result. The tax per product instead of a "added tax" subtotal. Still thank you for your time, thinking along and pointing out the cart.attribute.


When the subtotal is calculated on the shop frontend you just submit it with the cart by either rendering the html input value, or modifying the value with javascript, or as on the fly as part of the ajax request. 

Problem Solved? ✔️Accept and ? Like the solution so you can help others.
Buy me a coffee ☕ paypal.me/paulnewton or donate to eff.org
Confused? Busy? Buy a custom solution paull.newton+shopifyforum@gmail.com
0 Likes