Check if a product variation allows backorder
-
Hello! I am working on a WooCommerce site. What I’m trying to do is the following:
When a product is variable, its variations allow for backorder, right? So I need to check if X variation allows for backorder when the user selects that specific variation (for example, size Medium color Blue), and then execute any code I want. If the variation does not allow for backorder, then don’t execute the code.
So far, I could only achieve this:
//If a product allows backorder, modify add to cart button add_action( 'woocommerce_after_add_to_cart_form', 'edit_backorders_allowed' ); function edit_backorders_allowed() { global $product; //Check if allows backorder if ( $product->backorders_allowed() ) { ?> <style type="text/css"> .single_add_to_cart_button { display: none !important; } .quantity { display: none !important; } </style> <button id="reservar" type="submit" class="button alt">Reservar producto</button> <?php } }
But this does not apply to variations. It works only if the entire product allows backorder, and is not specific to variations.
I’ve also found this bit of code on the internet:
if ($product->is_type( 'variable' )){ // Get the available variations for the variable product $available_variations = $product->get_available_variations();
I don’t really know if this is useful for what I’m trying to do, but maybe any of you guys can tell me.
As you can see, I don’t know much about WP or WooCommerce (since probably there is a class or something that checks that same thing but on variations). But this functionality is really important for my client, so any help is much appreciated!
- The topic ‘Check if a product variation allows backorder’ is closed to new replies.