2
Answers
Vote up!
0
Vote down!

disable product variations on any single variation's stock hitting zero

I have Commerce Kickstart2. and I have installed/enabled the commerce_stock module.

I got a nice rule to disable the product SKU (i.e. one product variation) when the stock hits zero:

{ "rules_out_of_stock_disable_active_product" : {
"LABEL" : "Out of Stock Disable Active Product",
"PLUGIN" : "reaction rule",
"ACTIVE" : false,
"OWNER" : "rules",
"REQUIRES" : [ "rules", "entity" ],
"ON" : { "commerce_product_presave" : [] },
"IF" : [
{ "entity_is_of_type" : { "entity" : [ "commerce-product" ], "type" : "commerce_product" } },
{ "data_is" : { "data" : [ "commerce-product:commerce-stock" ], "value" : "0" } }
],
"DO" : [
{ "data_set" : { "data" : [ "commerce-product:status" ], "value" : 0 } }
]
}
}

but my issue is this: I have to disable all variations with the same commerce_product:title when any one of the variations hits zero stock.

For instance, I may have a product "mythingy" with two variations having SKU's "mythingyblue" "mythingygreen". If someone buys all the "mythingyblue" items, I need to set the "mythingygreen" and "mythingblue" variations to disabled...

So, what I think I am saying is I want a rule to disable an SKU, based on a calculation.
The calculation will have to loop on the full list of commerce_products (I don't have many),
-select only those for which the title matches the current product (e.g. all products with title "mythingy"),
-determine if **any** of the products with title "mythingy" have hit stock=zero
-supply that information back as the result of the calculation
The rule will then use this result to potentially disable the SKU.

Any ideas would be greatly appreciated since I have been bashing my head against a brick wall for 3 days. I have gone between rules, components, loops conditional components, ugh.. I am stuck!! (help!)

Asked by: electroweak
on April 27, 2014

2 Answers

Vote up!
0
Vote down!

Okey.. I think I have solved this. I will post here the actual rules and components I used once I've finalised them, but basically I made a rule which, before saving a product,
-does a fetch-entity on commerce-product (this gets a list of all active product variations)
-checks the product variation has stock=0 (otherwise, it will not loop below, or continue)
-Loops over this list (of commerce-products from fetch-entity)
in the loop, it calls a "Rule Component" for each member of the list.
-The "Rule Component" takes two parameters:
-1 the "list-item" being the member of the list above
-2 the product title which we decided had zero stock
-The "Rule Component" then checks each provided "list-item" against the title provided (the one with stock=0).
-If the titles match in the Rule Component, then the product variation's status is set to 0 (i.e. deactivated).

NB: This solution requires my product variations (e.g. mythingyblue and mythingygreen to have the same title, which I don't think is a problem.

I'll post my rule and rule component here as soon as I can.

Answer by: electroweak
Posted: Apr 28, 2014
Vote up!
0
Vote down!

Solution.....

NB: I had to set this to occur on the event "After updating an existing commerce product" so that it worked.
NB: As-is, it loops only over 100 variations. You can extend that, if needed.
NB: There are messages added inside the rule and component, which you can disable if needed.

{ "rules_xyz17" : {
"LABEL" : "xyz17",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules", "entity" ],
"ON" : { "commerce_product_update" : [] },
"IF" : [
{ "entity_is_of_type" : { "entity" : [ "commerce-product" ], "type" : "commerce_product" } },
{ "entity_has_field" : { "entity" : [ "commerce-product" ], "field" : "title_field" } },
{ "data_is" : { "data" : [ "commerce-product:commerce-stock" ], "value" : "0" } }
],
"DO" : [
{ "entity_query" : {
"USING" : {
"type" : "commerce_product",
"property" : "status",
"value" : 1,
"limit" : "100"
},
"PROVIDE" : { "entity_fetched" : { "entity_fetched" : "Fetched entity" } }
}
},
{ "LOOP" : {
"USING" : { "list" : [ "entity-fetched" ] },
"ITEM" : { "list_item" : "Current list item" },
"DO" : [
{ "drupal_message" : { "message" : "hi the list-item:title_field: [list-item:title_field] list-item:sku: [list-item:sku] list-item:status: [list-item:status]" } },
{ "component_rules_xyz17_rulecomponentd" : {
"mycommerceproductmachinelabel" : [ "list-item" ],
"myproductinplaymachinename" : "[commerce-product:title-field]"
}
}
]
}
}
]
}
}

and the component:
{ "rules_xyz17_rulecomponentd" : {
"LABEL" : "xyz17-rulecomponentd",
"PLUGIN" : "rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules" ],
"USES VARIABLES" : {
"mycommerceproductmachinelabel" : { "label" : "mycommerceproductlabel", "type" : "commerce_product" },
"myproductinplaymachinename" : { "label" : "myproductinplaytitlelabel", "type" : "text" }
},
"IF" : [
{ "entity_has_field" : {
"entity" : [ "mycommerceproductmachinelabel" ],
"field" : "title_field"
}
},
{ "entity_has_field" : {
"entity" : [ "mycommerceproductmachinelabel" ],
"field" : "commerce_stock"
}
},
{ "text_matches" : {
"text" : [ "myproductinplaymachinename" ],
"match" : "[mycommerceproductmachinelabel:title_field]"
}
},
{ "NOT data_is" : {
"data" : [ "mycommerceproductmachinelabel:commerce-stock" ],
"value" : "0"
}
}
],
"DO" : [
{ "drupal_message" : { "message" : "--inside cpt: mycpml:title is [mycommerceproductmachinelabel:title] stock is [mycommerceproductmachinelabel:commerce_stock] status is [mycommerceproductmachinelabel:status]\t title_field is [mycommerceproductmachinelabel:title_field]" } },
{ "drupal_message" : { "message" : "--inside cpt2 myproductinplaymachinename:value [myproductinplaymachinename:value] equals mycommerceproductmachinelabel:title_field [mycommerceproductmachinelabel:title_field]" } },
{ "data_set" : { "data" : [ "mycommerceproductmachinelabel:status" ], "value" : 0 } }
]
}
}

Hope it helps!!!

Answer by: electroweak
Posted: Apr 29, 2014