Bundle discount: if items are added from a bundle page, keep them in a bundle when added to cart

Tourist
7 0 1

The case I'm dealing with is very specific and I haven't managed to find any help in the community so far.
I'm working on a Bundle Discount feature where combinations of certain product ID's give a % discount when added to the cart. 

I have two types of pages on the website:
1. Product page for each single product (product.liquid template).
2. Bundle page where items are shown and added to the cart as a bundle and users know they will get the pack discount (bundle template).

If they add single items that are grouped as a bundle in the script, they will get the discount - so far so good.
However, imagine they are on the bundle page and after adding the bundle products, they add another single item which happens to form a pack with a product from the bundle page. The problem is, the discount will be applied to whichever bundle is first in the CAMPAIGNS array, so that might break the grouping of the bundle page products. This is the BundleDiscount object (= a single bundle) and the CAMPAIGNS array that contains all the bundles according to product ID:

 

class BundleDiscount < Campaign
  def initialize(condition, customer_qualifier, cart_qualifier, discount, full_bundles_only, bundle_products)
    super(condition, customer_qualifier, cart_qualifier, nil)
    @bundle_products = bundle_products
    @discount = discount
    @full_bundles_only = full_bundles_only
    @split_items = []
    @bundle_items = []
  end
  # some other code here that I didn't think is relevant
  end
CAMPAIGNS = [
  # single bundle: 
  BundleDiscount.new(
    :any,
    nil,
    nil,
    PercentageDiscount.new(
      20,
      "20% discount applied"
    ),
    true,
    [
     # the following product ID's form the bundle:
      {:type => "pid", :qualifiers => ["2631126679652"], :quantity => "1"},
      {:type => "pid", :qualifiers => ["2631131365476"], :quantity => "1"} 
    ]
  ),
# other bundles here ...
].freeze

What I want to do is KEEP the bundle products added from the bundle page grouped.

So I need to create a conditional -> if products are chosen from the bundle page, then keep them grouped within cart, even when other products eligible for bundle discount are added. 
I have no idea whether to modify the script or implement the conditional in the template.liquid files? Any directions and ideas would be of help. THANKS!


0 Likes