d
Topic
Posts:
5
27 days ago

Script Editor App - Removing product/s from cart?

I'm wondering if anyone knows of a way to delete/remove/unquantity the line_items in a cart via the script editor. Even emptying the cart would work!

Input.cart.line_items.each do |line_item|
  product = line_item.variant.product
  next if product.tags.index('some-tag') === nil?

  # this does not work since line_items can't have a qty of 0?
  # we were able to decrease quantity to 1 like this 
  line_item.split({ take: line_item.quantity })

end

Output.cart = Input.cart
i
Replies
Posts:
5
11 days ago

Quick update on this! There is a way of acheiving what we wanted but not by manipulating cart contents/quantity. You can remove checkout or shipping options if a product with a tag is present in the customer's cart. See below:

payment_gateways = Input.payment_gateways

Input.cart.line_items.each do |line_item|
  product = line_item.variant.product
  if product.tags.index('no-checkout') != nil
    payment_gateways.delete_if do |payment_gateway|
      payment_gateway != ''
    end
    break
  end
end

Output.payment_gateways = payment_gateways

Hopefully this is helpful for someone in the future!