Splitting Cart rows/lines

fwallace
Shopify Partner
37 0 3

In the Shopify Script Editor, it is a common use case to "split" cart lines/rows when running a "2 for $25" sale.

 

Products can have different prices, but in bundles of two they need to be obviously, $12.50 each so the Cart Transform not Discount Function would need to be called.

 

A sample cart can have two cart lines of eligible products, one with quantity 3, another with quantity 2. Thus the cart would need to have an additional line added, and one of the lines quantity reduced. Is this possible or is their something like the "split" function in the Shopify Script editor?

 

Example from Shopify Github repo for Script Editor below:

 

cart.line_items.each do |line_item|
      product = line_item.variant.product
      # If not perfect, split the cart with elgible items into QTY of 1
      if line_item.quantity > 1 && !product.gift_card? && product.tags.include?(promo_code)
        item = line_item.split(take: line_item.quantity-1)
        # Insert the newly-created item in the cart, right after the original item
        position = cart.line_items.find_index(line_item)
        cart.line_items.insert(position + 1, item)
      end
    end

Again, I can't use the Discount API as product prices might be $20 for one product and $25 for another if eligible they would need to be reduced to $12.50, and I might need to the split as above (Production script for $2 for 25 promo.

 

Is this possible?

 

Thanks.

Replies 2 (2)

Sweet_Savior_3
Shopify Partner
1201 96 116

Hello @fwallace 

Is that all the product price will be same. For that we need to check if it is possible or not.


Don't forget to like and accept the solution to help others store owners.
Looking for an expert or Have any queries ? Send query to the Inbox
Coffee Tip ? Pay to shopify.dev.34@gmail.com
From Less To Further !!!
fwallace
Shopify Partner
37 0 3

Hi @Sweet_Savior_3 

 

Products could have different prices originally, if updated in the cart line they would have a new lower price.

 

I have this in the Cart Transform "partially" working, in other words if the total of eligible items (correct tag) is even, then I can transform the price to the new price (12.50 each).

 

My issue is I am having trouble "splitting" the cart, when I have a total of odd eligble items. 

 

Example, the sale is "2 for $25" and customer has added 3 items eligible on one line, and four on another. I want to (and can do this in the Script Editor per above), "split" the line and a quantity of one of one the above lines (does not matter which in the above case) with a price that will NOT be changed, and decrement the quantity of the line being split by one. So IF I modifying the line with quantity of 4/four, I would add another line to the cart with the same properties as the line with four/4, just change the quantity to one/1, and then decrement the quantity of the line with four/4 to three/3.

 

Again this is a function supported in the Script Editor, and many merchants use it to basically blow out inventory odds/ends with limited quantities by incentivizing customers to buy multiples (can be different products).

 

I need to know if this behavior (decrementing a cart line quantity) is possible? Since it fails for me now.