简体   繁体   中英

Shopify script to allow checkout only when all products contain same tag

I'm writing a LineItem script for Shopify Plus that checks to ensure that all line items share at least one product tag which includes the word "collection".

Orders on our store can only be placed if all products are from the same collection, so we are tagging each product in the collection with a specific tag. Some products are included in multiple collections.

My logic is:

  • Loop through all line items and:
    1. Push the tags that contain "collection" into an all tags array
    2. Count the line items in the cart.
  • Copy unique values to a second array
  • Double loop through the unique values array and the all tags array to count the number of times the tag appears in the cart
  • If the number of times a tag appears equals the quantity of line items, then it's 'ok to checkout'
quantity_products = 0
all_product_tags = ''
ok_to_checkout = false

all_product_tags = Input.cart.line_items.select do |line_item|
  tag = line_item.variant.product.tags
  quantity_products += 1
end

unique_tags = all_product_tags
unique_tags | split: ' ' | uniq | join: ' '

unique_tags.each do | cart_tags |
  count = 0
  all_product_tags.each do | all_tags |
    if cart_tags = all_tags
      count +=1
    end
  end
  if count = quantity_products
    ok_to_checkout = true
  end
end 

I'm having trouble differentiating between using Liquid and Ruby from tutorials online. I get an error when I try to make the unique array.

I saw this article, but some of my products are included in multiple collections, so it doesn't work: Allow shopify cart to checkout only if all cart items have same specific tag

The one and only thing you can do that makes any sense to me, is to iterate the cart. The collection tag of the first item becomes the gold standard. If any of the next items do not match that gold standard, that means the product is not from the same collection. NOW WHAT? Are you gonna delete the item? You cannot just stop a checkout. You cannot flag items except by adding a tag to the offending item, which the CART would then show as illegal. And then you need Liquid to ensure an illegal cart has not button to checkout. Extremely weird pattern you are playing with there.

This is not so much your problem with Ruby or Liquid, but how you are going to steer the customer correctly into building a cart that passes inspection for one, and how to correct a bad cart. You likely do not want to auto-remove offending items, but you also do not want to submit bad carts. Therefore you have almost no choice but to implement this pattern of show/hide the checkout button on the cart, based on a tag you ADD to the offending products, and then ensure they are easy to see, and easy to remove.

I am betting your actual cart conversion rate plummets with this pattern.

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