Line item discount not working when customer logs in

Line item discount not working when customer logs in

clarib
Visitor
1 0 0

Hi

I have added the below line item script which

1) Checks if the customer is logged in

2) Checks if the customer has a particular tag 'VIP'

3) Checks the product is not a gift card/not on sale/includes a tag 'VIP'

Then applies a 10% discount

 

I have the following outcome on the frontend
- Customer is logged in with correct tag => Discounts applied correctly

- Customer logs out => Discounts removed immediately as expected

- Customer logs back in => Discounts do NOT reappear (the discounts only appear again after the customer adds or removes something from the cart)

 

Is this the expected behaviour?  Why would the script run correctly/immediately when a user logs out but not when they log back in?  If so, how can I force the script to run again?

 

customer = Input.cart.customer
if customer
if customer.tags.include? 'VIP'
Input.cart.line_items.each do |line_item|
product = line_item.variant.product
next if product.gift_card?
next if line_item.variant.compare_at_price
next unless product.tags.include? 'VIP'
next unless customer
line_item.change_line_price(line_item.line_price * 0.90, message: "BFF Discount")
end
end
end


Output.cart = Input.cart

 

Reply 1 (1)

Stephen2020
Shopify Partner
19 2 7

Hi,

 

you can force a recalculation by updating the cart note via the AJAX API. With jQuery such a request could look like this.

 

 

var note = (note = $('textarea[name="note"]').val()) ? note : '';

$.post({
  url:'/cart/update.js',
  data:'note=' + note,
  dataType: "json"
}).then(callback);

 

 

 

I.e.: The current cart note is simply read and sent to the cart again.

 

Afterwards, of course, you have to reload the shopping cart. This is behind what I have now simply called "callback".

 

We have done a lot of tinkering related to things like that because we have implemented the entry of the discount code in the AJAX cart, whereby the discounts are calculated by Shopify scripts and displayed directly in the cart.