• 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!

    • This topic was modified 6 years, 11 months ago by brundea.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi @brundea

    If I’ve understood your requirements correctly then this should be possible within the variation settings.

    e.g. You would select each variation dropdown and tick the ‘manage stock’ checkbox

    Then to Allow Backorders for a particular variable product
    then if you want the customer to be able to backorder a variable product you set the ‘Allow backorders?’ dropdown to ‘Allow, but notify customer’ and set the ‘stock status’ dropdown to ‘In Stock’

    This will indicate ‘Available on backorder’ when you view the product variable in the shop

    Then don’t Allow Backorder for a particular variable product
    then if you don’t want the customer to be able to backorder a particular variable product you set the ‘Allow backorders?’ dropdown to ‘Don’t Allow’ and set the ‘stock status’ dropdown to ‘Out of Stock’

    This will indicate ‘Out of Stock’ when you view the product variable in the shop

    I hope this helps.
    Regards
    SteveB

    Thread Starter brundea

    (@brundea)

    Hi @pxwm

    Yes! I know this is possible. Maybe I haven’t explained myself correctly. What I want to know is how to check if a selected variation allows for backorder or not (in code). If yes, then execute a piece of code. If not, do nothing.

    Why do I want to do this? Because I don’t want to use the default backorder system that WooCommerce provides. I want to change the “Add to cart” button for a “Backorder product” button, which will direct you to a form that you fill and send an e-mail asking for reservation of that product. This part isn’t important, since I know how to code that.

    The ‘perfect code’ I’m looking for would be something like this:

    add_action( 'woocommerce_after_add_to_cart_form', 'edit_backorders_allowed' );
    function edit_backorders_allowed() 
        {
            
            global $variation;
                
                //IF VARIATION ALLOWS BACKORDER, EXECUTE CODE
                if ( $variation->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
                }
        }
    ?>

    Thank you!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Check if a product variation allows backorder’ is closed to new replies.