Skip to content
Advertisement

Rename “Select options” button on products archive for sold out products

I have a shop and each product that’s sold out is still referring to the product page despite it’s unavailability. The button “product options” still exists. So I found this code that’s supposed to change the “add to cart” button (which in my case is mostly ‘select options’ because most of my items are variable). I want to change the text on this button for sold out products only. I want it to state “Special order” or something. I want to refer them to an external page instead.

How can I do so?

add_filter( 'woocommerce_product_add_to_cart_text', 'customizing_add_to_cart_button_text', 10, 2 );
add_filter( 'woocommerce_product_single_add_to_cart_text', 'customizing_add_to_cart_button_text', 10, 2 );
function customizing_add_to_cart_button_text( $button_text, $product ) {


    $sold_out = __( "Sold Out", "woocommerce" );

    $availability = $product->get_availability();
    $stock_status = $availability['class'];

    // Only for variable products on single product pages
    if ( $product->is_type('variable') && is_product() )
    {
    ?>
    <script>
    jQuery(document).ready(function($) {
        $('select').blur( function(){
            if( '' != $('input.variation_id').val() && $('p.stock').hasClass('out-of-stock') )
                $('button.single_add_to_cart_button').html('<?php echo $sold_out; ?>');
            else 
                $('button.single_add_to_cart_button').html('<?php echo $button_text; ?>');

            console.log($('input.variation_id').val());
        });
    });
    </script>
    <?php
    }
    // For all other cases (not a variable product on single product pages)
    elseif ( ! $product->is_type('variable') && ! is_product() ) 
    {
        if($stock_status == 'out-of-stock')
            $button_text = $sold_out.' ('.$stock_status.')';
        else
            $button_text.=' ('.$stock_status.')';
    }
    return $button_text;
}

Advertisement

Answer

The following snippet may guide you through the right path.

If the product is a variable product, check the stock status of all variations and get the value to an array. If all the variations of a variable product are out of stock, then the text can be changed to Sold Out. Also you can change the text based on the availability of variations in a variable product.

if ( $product->is_type('variable') )
    {
        foreach ($product->get_available_variations() as $key => $value){
            $stock[] = isset( $value['is_in_stock'] ) ? $value['is_in_stock'] : true;
        }
        
        if( empty( array_filter( $stock ) ) ){
            $button_text = __( "Sold Out", "woocommerce" );
        }
    }
}

See the full snippet below.

add_filter( 'woocommerce_product_add_to_cart_text', 'customizing_add_to_cart_button_text', 10, 2 );
add_filter( 'woocommerce_product_single_add_to_cart_text', 'customizing_add_to_cart_button_text', 10, 2 );
function customizing_add_to_cart_button_text( $button_text, $product ) {

   $stock = array();

    // Only for variable products on single product pages
    if ( $product->is_type('variable') )
    {
        foreach ($product->get_available_variations() as $key => $value){
            $stock[] = isset( $value['is_in_stock'] ) ? $value['is_in_stock'] : true;
        }
        
        if( empty( array_filter( $stock ) ) ){
            $button_text = __( "Sold Out", "woocommerce" );
        }
    }
    // For all other cases (not a variable product on single product pages)
    elseif ( ! $product->is_type('variable') && !$product->is_in_stock() ) 
    {
        $button_text = __( "Sold Out", "woocommerce" );
    }
    return $button_text;
}
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement