How to add custom attribute to all outofstock and all instock products automatically?

by kh1   Last Updated October 19, 2019 11:08 AM - source

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;
}
Tags : php woocommerce


Related Questions


How can add and preview product Frontend

Updated March 26, 2015 12:02 PM


How to add "Reply-to" in Woocommerce

Updated April 04, 2015 15:03 PM


WooCommerce epdq gateway Change order status

Updated April 06, 2015 19:03 PM