Check if one variation is enabled or disabled in WooCommerce

One of my clients had the need to sometime disable one variation of many, but to avoid deleting and adding again they use en little checkbox Enable in the variation setting. Really simple!

But not equally simple to find out how to check that status in code it turned out. But after digging in the WooCommerce source I found out it uses the actual status of the “product”, i.e. every variation is kind of a product, but with a parent and some other stuff. So this is how you check that Enable checkbox:

$variation = wc_get_product($variation_id);
if ($variation->get_status( 'edit' ) == 'publish') {
  // do something if enabled
}
if ($variation->get_status( 'edit' ) == 'private') {
  // do something if NOT enabled
}       

So as you can see, the code is REALLY simple, a smart way of the WooCommerce team to reuse already built functionality. But it took a little while to find after googling for attributes, properties, meta-data and other stuff. Not so much on the web on this topic.

Comments

Cart
Sign in
Loading...