0

What I want to achieve is to add the custom attribute "instock" to all in-stock products and "outofstock" for products that out of stock right now. Why? Because I want to add those attributes to filter plugin, that doesn't support sorting by stock status

I think construction will be something like this, but I don't know how to add attributes ( Also, i`m pretty sure, that this hook woocommerce_get_availability is not the best solution to get actual statuses all the time

add_filter( 'woocommerce_get_availability', 'woocommerce_stock_availability_attributes', 1, 2);
function woocommerce_stock_availability_attributes( $availability, $_product ) {
   if ( ! $_product->is_in_stock() ) {
       //some magic stuff
   }
  return $availability;
}
0

WooCommerce already saves a custom metadata called _stock_status. This metadata can have these values:

instock
outofstock
onbackorder

You don't need to create your own metadata as that would be redundant and more difficult to keep in sync with WooCommerce data.

  • Yes, but the question is how to add to all instock products custom attribute "instock", because like i said before - filter plugin, that doesn't support sorting by stock status – kh1 Oct 19 at 11:31
  • Custom metadata have a meta_key and a meta_value. WooCommerce saves already a metadata with key = _stock_status and values as listed in the answer. Adding your own custom metadata would not make the filtering plugin work. So the question should be how to make the filtering plugin work with WooCommerce metadata. To answer this you should give more details about what plugin you are talking about. – freejack Oct 20 at 9:54

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.