I was able to change CSS for when stock is completely out thanks to the "out-of-stock" class. However, I'd like to also change CSS if stock is almost out. The only thing I could think to do is write a views-view-field--add-to-cart-form.tpl.php template. Unfortunately, I couldn't figure out how to check the value of the stock field. Basically, I couldn't decipher the print_r of $row to determine the format of the $field variable.

Using $field['commerce_stock']['und'][0]['value'] gives this error:

Cannot use object of type commerce_cart_handler_field_add_to_cart_form as array

Comments

rfay’s picture

Just brainstorming here, but you might try a technique based on http://www.commerceguys.com/resources/articles/216; use rules to determine when stock has fallen below a particular value, and populate a field (that you create) based on that. Call it "low_stock_notification". Then use CSS or whatever to key on that low stock notification field and deliver the message to your users.

guy_schneerson’s picture

You should use entity_metadata_wrapper() to access the data

If the rules aproch docent work you can probebly do this using hook_form_alter

guy_schneerson’s picture

@Mon Reveur, not sure about what you got from the print_r() but if $field['commerce_stock'] is the stock field it looks correct however if $field is the product it should be:
$field->commerce_stock['und'][0]['value'];
or
$product_wrapper = entity_metadata_wrapper('commerce_product', $field);
$stock = $product_wrapper>commerce_stock->value();

Also if you happen to be using the "Display suite" module you may be eable to do it using a Display field as those use tokens so can write somthing like
if ([commerce-product:commerce-stock] < 5) { ..
however i am not sure if this would be straight forward as the relations of the product and product display are not straight forward

I think that if you don’t mind adding a new "low_stock_notification" field to your product @rfay approach may be the easiest.

if you do manage to get this working please let us know how

farald’s picture

I used 2 rules & 2 fields, mostly following the tutorial vid mentioned in #1.
Worked like a charm.

Use rfay's feature http://drupal.org/sandbox/rfay/1229306 to quickly get the rules needed.
This only sends a mail. You can do what I did, make the rule set a field on the product instead of sending mail / displaying a message, allowing for quickly creating rss / exports of the products low on stock.

guy_schneerson’s picture

Status: Active » Closed (fixed)