Currently the module changes add to cart form button to disabled if selected element is out of stock. Would it be possible to filter out of stock items from the drop down altogether?
Should this happen in Commerce Simple Stock if so? I'll be looking into that this week but I want to know if this is something that could get into module or you have already thought about it and it's not going to happen.

Comments

guy_schneerson’s picture

@philipz Would be a cool feature not sure how simple it will be to implement with attributes may be possible to do in the hook_form_alter(), if i get a chance will play around with it, but will welcome both patches and ideas about how it should be implemented.
It should be done in the main commerce_stock module, i have a real issue with how i have implemented the relationship of the main module and simple stock as stock should hide the implementation of the SS module, however the triggering of the checks (rules events) is performed by the main commerce_stock module so you should be fine, if you manage to get most of it working but have problems integrating into rules i can help.

iaugur’s picture

One approach would be to use a rule to make out of stock items disabled (status=0)
and then they wouldn't appear in the list!
A companion rule could set them back enabled when stock is back > 0
It would be nice too to have the last product unpublish the display when it goes out of stock

philipz’s picture

I'm looking through 7.x-2.0-alpha2. I see that changing selected dropdown element triggers rules event that sets the cart form state based on the product stock level. If we had only available products in the drop down we wouldn't need to validate stock at this point as it gets validated on add to cart anyway.

So first I'm going to remove the commerce_stock_cart_state_validate_options and replace it with a function that checks products one by one and filtering them out if stock is 0. This new function could maybe reuse rules already in the module or do the filtering without rules at all. The second approach is simpler for me and might be faster performance wise I think.

It's late so I'll start tommorow and I'll appriciate a lot any thoughts from you @guy_schneerson.

@iAugur making a rule that sets out of stock items disabled seems like great idea.

guy_schneerson’s picture

@philipz if its for your own site than go for what ever works for you. If this was to be made a feature of stock it would need to be a configurable option probably per product bundle and respect the stock override also the stock check on change gives a better UI for when pages are open for a long time and stock has run out meanwhile.

@iAugur i think this is probably the best generic approach as it allows users to customize the rules if they need to, would make a great sub module. Same as above will need to check stock enabled for the bundle and the override but should be easy to implement.

philipz’s picture

I would like it to be a feature of stock and as you described it I agree completely. But first I have to make it work on my own site and I'm starting simple. I'll let you know if I have something to show. I will not remove checking the stock on change but maybe this could be an option too.

srgk’s picture

this feature is just what i need, good luck with it
subscribing

philipz’s picture

StatusFileSize
new1.09 KB

This is basic functionality patch. It removes out of stock products from add to cart dropdown. Next I'll:
1. Make this into an option per product type.
2. Respect stock override.

guy_schneerson’s picture

Hi philipz nice work,
other things to consider :
Integration with rules for checking
How to handle products that use attributes
configuration should be on the commerce_stock not the ss

the above may be difficult at the moment, i am planning on a new version of stock soon (a major upgrade of 2 or a 3 brunch) that will be a more object oriented API that will allow the stock module to make stock queries without knowing about the implantation.

for now i think that the approach in #2 may be more practical as it is fully rule based, however if you need this changes do post a patch so others can use it and ill be happy to add it in as a feature if you do manage to fully integrate it.
note that this will be easier on the version 1 as it has no sub module architecture.

philipz’s picture

StatusFileSize
new1.09 KB

Fixed two mistakes in patch #7.

philipz’s picture

@guy_schneerson thanks! :)

I have no experience in regard of products with attributes. Do they have separate stock field per attribute combination? I remember I did some coding for attributes and stock in Ubercart a year ago and I had some good results with it.

Do you have any timeline on 3.x roadmap you could share? I feel that the next branch will be as awsome as the whole commerce and what you probably wanted it to be from the start :) I'll try to finish my approach as described in #7 but now I have less motivation to do it.

guy_schneerson’s picture

@philipz products with attributes are controlled by the configuration of product properties its just a different way of formatting the output, instead of a single drop-down you get one for each attribute like colour and size.

time line for the next major upgrade is probably after DrupalCon Munich (will try and organize a bof if you are around)

Don't wont to discourage you just trying to be transparent and i think that this discussion and your work will improve both of our knowledge of Drupal Commerce and produce a better stock module.

srgk’s picture

hi!
is there any progress on the out of stock products with attributes?

i'm not a very experienced Drupal user, but i have tried a different approach, and i think that it's working...

i added the following function to my module, to make sure that each time a commerce product is saved it's in stock (the field 'commerce_stock'), and if not - the product is disabled (and thus disappears from the product displays).

i have also checked if saving with entity_metadata_wrapper (like in the Simple Stock Rules) triggers the hook_entity_presave, and it looks like all is ok.

please feel free to correct me if anything


function MYMODULE_entity_presave($entity,$type)
{
	if ($type=='commerce_product')
	{ 
$products_stock=field_get_items('commerce_product',$entity,'commerce_stock');
 if($products_stock['0']['value']<=0)
  {
  	$entity->status=0;
  }
  	}
}

guy_schneerson’s picture

Hi @srgk your approach is the same as #2 proposed by @ iAugur only uses code instead of rules

guy_schneerson’s picture

Assigned: Unassigned » guy_schneerson

For this to make it into the module we need to create a new stock event and action something like

Event "Stock add to cart preprocess product"
Action - Hide product

test if this can be used with both attributes and options, action can also be used to add the out of stock that existed in version 1.

I am assigning to myself as i think this is an important upgrade (so its easy to find), but patches are welcome

akalata’s picture

A different approach, but here's an exported rule that seems to be working fine. It fires on entity_presave, so it captures administrator actions as well as stock decrements.

{ "rules_out_of_stock" : {
    "LABEL" : "Out of Stock",
    "PLUGIN" : "reaction rule",
    "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 } }
    ]
  }
}
kirie’s picture

Thanks @akalata!

Here is the accompanying rule which activates a product once it is back in stock :)

{ "rules_stock_enable_product_when_back_in_stock" : {
    "LABEL" : "Stock: enable product when back in stock",
    "PLUGIN" : "reaction rule",
    "TAGS" : [ "Emoda" ],
    "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" ], "op" : "\u003E", "value" : "0"
        }
      }
    ],
    "DO" : [
      { "data_set" : { "data" : [ "commerce-product:status" ], "value" : 1 } }
    ]
  }
}
gynekolog’s picture

And is there any way to unpublish product display if all referenced products are sold out (or are disabled)?

edit: http://drupal.org/node/1461442#comment-5675126

my bad

kirie’s picture

Yeah, I was about to say views :) I just used views to not show product displays if it didn't have a active or in-stock product attached to it. I did not want to unpublish the product displays just in case a customer wanted to view a purchased product (or had a product display bookmarked).

mrackham’s picture

when I import either of the rules from #15 and #16 I get the error:

Integrity check for the imported configuration failed. Error message: Data selector commerce-product:commerce-stock for parameter data is invalid

Is there something that I am doing incorrectly? I am using the default install of Drupal Commerce Kickstart v2

gillisig’s picture

I guess I got the same error when I first tried it, I can't remember exactly what I did to fix it though, I think the token module was not activated or something so make sure to check that all the needed modules are active.

mrackham’s picture

the error did not recur when I installed commerce_stock v2

however, the same error does occur if you install commerce_stock v2 and you have not yet enabled stock management for any products yet as detailed here: http://drupal.org/node/1462952

Thank you s2sz for your reply

summit’s picture

Hi,
And how about updates through feeds? Are they also covered if a feed update makes the stock 0?
Thanks a lot in advance for your reply!
Greetings, Martijn

lsolesen’s picture

Issue summary: View changes

Trying to import rule from #15 I get

Integrity check for the imported configuration failed. Error message: Data selector <em class="placeholder">commerce-product:commerce-stock</em> for parameter <em class="placeholder">data</em> is invalid..
guy_schneerson’s picture

Assigned: guy_schneerson » Unassigned

Just removing the assigned to me as you guys are doing all the work on this issue :)

Yuri’s picture

Anyone already using a working solution?