I am writing a Spree extension to allow certain items in the cart/order to be linked to each other.
A "setting" product can be associated with a "center stone" product. Eventually, there shall be constraints enforcing which things can reference each other, but that's not important yet.
Here's how I've changed the LineItem to include self references:
Spree::LineItem.class_eval do
has_one :center_stone, class_name: "LineItem", foreign_key: "setting_id"
belongs_to :setting, class_name: "LineItem"
end
...and the corresponding DB migration:
class AddSettingRefToLineItems < ActiveRecord::Migration
def change
add_reference :spree_line_items, :setting, index: true, foreign_key: true
end
end
What I need to accomplish next is to modify the "Add to Cart" form on the product page so that an item being added to the cart can get associated with an item that is already in the cart. How do I do this?
Use Case Example
Product A and Product B are both in my cart. I am looking at the page for Product C. I want to see the options:
- Add to Product A
- Add to Product B
- Add to Cart Alone
Clicking any of these options creates a Spree::LineItem for Product C as usual. If click the first two option, I also want the LineItem for Product C's setting_id to reference the LineItem for Product A in my cart.