Discount amount in checkout shows $0.00 when changing line item price

Highlighted
Tourist
27 0 2

Have a script that changes line item price if discount code is present.

The discount works, and the script returns no errors, but this discount value is always at $0.00 when the discount utilizes the script.  In the example below both a 20% and 30% have already been applied and are being reflected in the subtotal.  Original price is $169.00.

 

Is there a method for reporting the dollars saved original line-item total vs discounted line-item total? 

If there is a method, is it controlled via liquid in checkout, or through Shopify Scripts?

issue-example

 

 

 

 

 

0 Likes
Tourist
27 0 2

Update...

Tried figuring out the visibility involved in these methods and their impacts on checkout values, but didn't find anything definitive.

https://help.shopify.com/api/tutorials/shopify-scripts#line-item-methods

0 Likes
Shopify Partner
1 0 0

Wondering if you found anything on this, as I have the same issue. I've applied a discount via script editor, but the discount line in our cart is not reflecting the adjusted value.

I'm wondering if there is a way to set that value or a method in script editor that will adjust that value.

We're about to run a promotion and are worried that this will cause confusion for our customers at checkout. 

Thanks! 

0 Likes
Shopify Expert
19 0 1

We're also looking for a solution to this. Line Item Scripts can be very useful sometimes, but some of their use cases are also often hindered by confusing resulting displays to the customers.  If there were a field added to checkout showing the total savings from Line Item Scripts, I think that could take away a lot of the confusion.

As it stands, I haven't been able to alter the Subtotal/Discount/Shipping fields very well using client-side javascript, because the Checkout just immediately overwrites it when it re-calculates those fields itself.

I've taken to just adding a somewhat clunky "Additional Savings" field up above, below the line items, with this:

{% assign line_item_discounts = 0 %}
{% for line_item in checkout.line_items %}
	{% if line_item.original_line_price != line_item.line_price %}
		{% assign difference = line_item.original_line_price | minus: line_item.line_price %}
		{% assign line_item_discounts = line_item_discounts | plus: difference  %}
	{% endif %}
{% endfor %}
{% if line_item_discounts > 0 %}
	<script>
		$('.order-summary__section__content .product-table').after('<table class="total-line-table additional-savings-table" style="margin-bottom:20px;"><tbody><tr class="total-line total-line--additional-savings"><th class="total-line__name" scope="row">Additional Savings</th><td class="total-line__price"><span class="order-summary__emphasis">- {{ line_item_discounts | money }}</span></td></tr></tbody></table>');
		$(document).on('click','button.applied-reduction-code__clear-button',function() {
			$('.additional-savings-table').hide();
		});
	</script>
{% endif %}

Isn't perfect at all, in part because it relies on the checkout being revisited/reloaded after the discount code is applied, but looks something like this:

0 Likes
Shopify Expert
19 0 1

Ok, I've played with this a bit more today and may have a solution that works a little better than the above. It's able to add the line item discounts to the number already shown in the Discounts field, client-side. In addition, if a discount code is added and the discount is $0 off, it will hide the $0 if we're not able to calculate the line item discounts in Liquid yet.

In this example image, the discount code saves $0 while triggering a Line Item Script that saves $44.50. The $44.50 total discount appears in the Discounts field next to the code, while the Subtotal field gets $44.50 added on to it so the discount subtractions look correct to the customer:

Definitely test this code in your own environment and look to make any improvements you can, as I wrote this pretty quickly:

{% assign line_item_discounts = 0 %}
{% for line_item in checkout.line_items %}
	{% if line_item.original_line_price != line_item.line_price %}
		{% assign difference = line_item.original_line_price | minus: line_item.line_price %}
		{% assign line_item_discounts = line_item_discounts | plus: difference  %}
	{% endif %}
{% endfor %}
{% if line_item_discounts > 0 %}
	{% comment %}Add Line Item discount total to discount field and subtotal field, if discount field exists{% endcomment %}
	<script>
		$(window).load(function() {
			if ( $('.total-line--reduction .order-summary__emphasis').length ) {
				var current_discount = parseFloat( $('.total-line--reduction .order-summary__emphasis').html().replace('$', '') );
				var additional_savings = {{ line_item_discounts | money_without_currency }};
				var total_discount = (current_discount + additional_savings).toFixed(2);
				$('.total-line--reduction .order-summary__emphasis').html('- $' + total_discount);
				var current_subtotal = parseFloat( $('.total-line--subtotal .order-summary__emphasis').html().replace('$', '') );
				var new_subtotal = (current_subtotal + additional_savings).toFixed(2);
				$('.total-line--subtotal .order-summary__emphasis').html('$' + new_subtotal);
			}
		});
	</script>
{% endif %}

{% comment %}Hide discount amount when amount is $0.00{% endcomment %}
<script>
	$(document).on('click','button[type="submit"][data-trekkie-id="apply_discount_button"]', function() {			
		setTimeout(function() {
			if ( $('.total-line--reduction .order-summary__emphasis').length ) {
				var current_discount = parseFloat( $('.total-line--reduction .order-summary__emphasis').html().replace('$', '') );
				if( current_discount == 0) {
					$('.total-line--reduction .order-summary__emphasis').hide();
				}
			}
		}, 3000);
	});
</script>

 

0 Likes