3

I have about 500 product in my site powered by the commerce module. I need stock capabilities so installed the commerce_stock module. So far so good.

This module add a stock field and a checkbox to check Disable stock for this product in the product itself.

Thing is that stock field is set to 0,00 by default, then all the product are shown as Out of stock.

The solution is as simple as to mark the checkbox to check Disable stock for this product for all products. But I have 500 products, then this will be a waste of time...

Is there any way to do this programmatically? Any other idea to get this done?

I thing of doing this directly in the database with a sql query, but not sure if this is a good idea?

8

Well I can come out of two ways of doing this. One of them is Using the commerce API which is really good you just have to get all your 500 products with the following method:

 $query=new EntityFieldQuery;

    $query= $query->entityCondition('entity_type','commerce_product')->execute();

    foreach($query as $entity){
       $entity->commerce_stock[LANGUAGE_NONE][0]['value'] = 1;
       commerce_product_save($new_product);
    }

Or you can also do it on the database updating the value of the table field_data_commerce_stock:

update field_data_commerce_stock set commerce_stock_value=ANY_NUMBER;
2
$wrapper =        entity_metadata_wrapper('commerce_product',commerce_product_load($product_id));
$wrapper->commerce_stock->set("set the value");
$wrapper->save();
  • Thanks for the answer. In the future: more explanation is always nice. – joshmiller Mar 8 '16 at 14:43
1

I don't know exactly the module you mentioned. But check the schema. You should find a field attached with your Content type, and maybe you can execute a query thought Devel and PHP Execute block (db_query(...)).

In the content type fields configuration, if you set a default value, maybe it will set for all products the same value.

Or another solution is to install Views Bulk Operations module and you will be able to change on field for all nodes that you are looking for, but for that the module should be coded to support it.

1

You can do it with views bulk operation. Edit the product page view. To the view's fields add "Bulk Operations". Under "Selected Bulk Operations" check "Modify entity values".Save. Return to the product's page, select the products you want to modify, and on the Bulk Operations list choose "Modify entity values" click Apply. You can check off Stock under "Fields" and it will give you a field to enter the stock quantity you want and confirm.

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.