3

I've been trying forever to create a stock field for a product display that contains multiple product skus. I want to do this so that when each variation is purchased, it is subtracted from the total number of items in stock. Commerce Stock seems to let you track stock for individual skus, but not for one product with multiple skus. For example, I have a total of 100 shirts, but 10 shirts are pink, 10 are red, 10 are blue (hence, the different SKUs). That said, when 2 blue shirts have been purchased, I want the stock to drop from 100 to 98 on the product display. Out of the box, this isn't possible.

I tried using the answer in Synchronize commerce stock for product variations, but I'm unable to achieve the same result. I created a master stock text field on the product display, but I can't seem to set rules to subtract from the new field. Does anyone have clear instructions for doing this via rules?

In short: I am looking for a simple way to synchronize all product variant's stock in a single product display.

** UPDATE

I have SUCCESSFULLY obtained the nid of a product display, and then, fetched Product Display by that nid with the below rule settings. That said, when I try and add an action to set data for my custom stock field (product_stock) located on my content type Product Display, rules can't seem to find it? How is this happening?

enter image description here

FETCH ENTITY BY ID SCREEN

enter image description here

THE SELECTORS I DO GET WHEN I DO FETCH ENTITY BY PROPERTY:

enter image description here

  • Let me see your fetch entity by id screen. – No Sssweat Mar 28 '18 at 23:41
  • @NoSssweat See above edit :) ! – Brittany Mar 28 '18 at 23:43
  • Sorry my Rules skills are a bit rusty. I think you need to do "Fetch property by property" – No Sssweat Mar 28 '18 at 23:52
  • A big issue with your rule is that people can add items to cart, but may not necessary complete the process and pay. So you would be subtracting prematurely. – No Sssweat Mar 28 '18 at 23:54
  • 1
    @Pierre.Vriens Help, ha ha. I'm out of ideas - dying to know what the answer here is! – Brittany Apr 1 '18 at 21:58
1

First, you will need to add an entity reference field to your line items. This will reference the product display node.

Then using Views Rules create a line item view with a "Rules Display"

  • For "contextual filter" add Order ID.
  • For "fields"
    • Add the quantity
    • Add Node ID (nid) of the referenced Product Display Page.

Now in your rule simply add a view loop and pass the Order ID as a parameter.

Inside the View loop:

  • Fetch entity from ID, pass the NID from the view result.
  • Do Calculate a value. Get the fetched entity quantity - view result quantity

  • Set a data Value for fetched quantity field, calculated value.

  • Will try this today and let you know if it works! – Brittany Apr 6 '18 at 17:54
  • As luck would have it, I managed to accomplish the above without Views. At long last. Thank god. I'll post the solution below shortly. – Brittany Apr 11 '18 at 20:19

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.