1

I am selling tickets to a class at multiple price points (think student, adult, senior), and each class has limited seats. I have the product display working great using product reference with inline entity form - Multiple values. I can successfully checkout with each product variant.

I have run into a snag using Commerce Stock as the stock is per product, which in my case is each variant of the class. So say I have 30 seats, I need to be able to sell 30 of any product variant, but no more than 30 of all product variants total (per class, which is a single product display).

Is there a way for the stock to be per product display, not per product? If not, is there something I can invoke using Rules to subtract from all product variants stock when one is purchased?

Ultimately, I am looking for a way to synchronize all product variant's stock in a single product display.

2

Here's a relatively straightforward way to track stock based on a group:

Caveat Emptor: This will only work well if you have unique products per product display, if you try to re-use products (same ticket for multiple events) it could wreak havoc on your stock numbers.

Before starting: Download and enable "Rules Data Transforms" and "Conditional Rules"

  1. Create master stock field. Add a field to your Product Display called "master stock" or "open seats" or whatever makes sense for your use case.

  2. Clone rule. Find the rule that decrements stock on the product level, clone it, disable the original. If it is a component, you may need to locate the event rule that calls it and clone/disable that rule as well. Just depends on your setup.

  3. Edit new cloned rule. Inside your newly cloned rule, you are going to add an action that decrements the master stock.

  4. Calculate new Stock number. First, calculate the new stock number by subtracting one from the product's stock field. This will be the new stock number that we will set on the product display, and all of it's children.

  5. Load Product Display. Pull a product display from the line item field commerce_display_path. This can be tricky, which is why I had you install the Rules Data Transforms (link above).

    A. Add a condition Entity has field and we're checking that the line item has the field commerce_display_path (this works great with default add-to-cart settings, but may not work if your add-to-cart doesn't track the display node path).

    B. Add an action Explode Text under data transforms. You are exploding commerce-line-item:commerce-display-path using the delimited / and calling it "display_path"

    C. Add an action Convert data type under the first group called Data. Your target type is integer and the value to convert is display-path:1 (the second value after exploding) and we'll call this product_display_nid

    D. Add an action Fetch entity by id. Click the "Switch to data select" button and pull in the newly converted product-display-nid

  6. Set master stock. Set master stock value of product_display by adding an action Set a data value for the product_display's master stock field to your calculated new stock price.

  7. Create inline condition. Next we need to create a conditional to bring in the "field_product" (or whatever field you use for product references). This is why I had you install the Conditional Rules module. (Yes, this could be accomplished with core rules components, but this feels more straightforward.)

    A. Click Add Conditional

    B. Click Add if and let's make the conditional an Entity has field by loading the product-display node and looking for your product reference field. By default it's called field_product

    C. Click Add loop and loop over the product-display:field-product list and call it sibling_product

  8. Set all sibling products stock. Finally, we need to add a Set a data value action to set each sibling_product's stock field to the new value. Note, this will include the product that has been purchased.


There you go! Admittedly, this is a pretty naive solution. If your master stock says "10" then all of your products will be "10" and when one is purchased, they will all say "9." When there is only 1 seat left, there will still be 3 tickets that could be purchased.

  • 1
    Awesome, thanks for taking the time to answer so in-depth. I will check this out later today. Much appreciated! – Paul Feb 4 '16 at 15:59
  • I'm currently trying to implement this - newb question lol: How can I make the rule subtract from my master stock field? I can't seem to make this happen (it simply gives me the option to subtract from the 'stock' field). – Brittany Mar 23 '18 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.