Can we Access collection or liquid object on script editor (line items)

Bashir
Shopify Partner
3 0 0

Actually i am using the script for "BUY X QUANTITY OF A PRODUCT, GET $Y OFF for multiple products" and want to get the all products of collection in this script. In the below code i am using the static array of product id with variable products_needed. We would like to get these product id from particular collections.

Script Code:

DISCOUNTS_BY_QUANTITY = {
  3 => 10,
}

 products_needed = [348255822438,348255552655,349955944742]

Input.cart.line_items.each do |line_item|
 next unless products_needed.include?(line_item.variant.product.id)
 
  quantity, discount = DISCOUNTS_BY_QUANTITY.find do |quantity, _|
    line_item.quantity >= quantity
  end
  next unless discount

 # message = "#{discount}% off when buying at least #{quantity}."
  message = 'YOU SAVED TIER_AMOUNT BY BUYING 3 OR MORE'
  line_item.change_line_price(
    line_item.line_price * (Decimal.new(1) - discount.to_d / 100),
    message: message,
  )
end

Output.cart = Input.cart

0 Likes
Jason
Shopify Expert
10039 119 1876

Shopify Scripts doesn't support collection data so you'll need to use another way to match products. Tags is the common approach.

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
0 Likes
Bashir
Shopify Partner
3 0 0

can you please suggest other way to do the same thing. 

One more thing can we use the API call in script editor.

0 Likes
Jason
Shopify Expert
10039 119 1876

can you please suggest other way to do the same thing. 

Use product tags instead.

One more thing can we use the API call in script editor.

No

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
0 Likes