Search code examples
wordpresswoocommerceproductcart

Change the Add to Cart button text on the archive page when item is out of stock (backorder available) in Woocommerce


I would like to change the archive 'add to cart' button text for backordered items.

I've tried adding this:

add_filter( 'woocommerce_loop_add_to_cart_link', 'filter_loop_add_to_cart_link', 20, 3 );
function filter_loop_add_to_cart_link( $button, $product, $args = array() ) {
if( $product->is_on_backorder() ) return $button;

$button_text = __('Preorder', 'woocommerce');

}

..but it removes the 'add to cart' button on the in stock products and still shows 'add to cart' buttons on the backordered items instead of 'Preorder'.

Any solutions?

View incorrectly displayed buttons with the above code


Solution

  • try this code

    add_filter( 'woocommerce_loop_add_to_cart_link', 'filter_loop_add_to_cart_link', 10, 2 );
    function filter_loop_add_to_cart_link( $button, $product ) {
        if( $product->is_on_backorder() ){
            $button_text = __("Preorder", "woocommerce");
            $button_link = $product->get_permalink();
            $button = '<a href="' . $button_link . '">' . $button_text . '</a>';
        }
        return $button;
    }