Skip to content

Instantly share code, notes, and snippets.

@WPprodigy
Last active December 4, 2022 16:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save WPprodigy/b60f63b17bca033ff8753ee058e78b14 to your computer and use it in GitHub Desktop.
Save WPprodigy/b60f63b17bca033ff8753ee058e78b14 to your computer and use it in GitHub Desktop.
Change add to cart button text in WooCommerce if product is on backorder.
add_filter( 'woocommerce_product_single_add_to_cart_text', 'wc_ninja_change_backorder_button', 10, 2 );
function wc_ninja_change_backorder_button( $text, $product ){
if ( $product->is_on_backorder( 1 ) ) {
$text = __( 'Pre-Order', 'woocommerce' );
}
return $text;
}
@LPackman
Copy link

Tried this but its not working for me, maybe depreciated?

@katoen
Copy link

katoen commented Apr 23, 2019

Tested, works for me. thanks

@sambadbidari
Copy link

This worked with me, but have a question. Can we change the function to popup after that changed button is pressed?

@stevethebartender
Copy link

Worked perfectly for me!

Anyone know how to replicate on the archive pages?

@stevethebartender
Copy link

Any chance you can explain how to add it to the existing code? I have no idea how to add this unfortunately..

I'm trying to change the Add to Cart buttons for Backordered items only - from "Add to Cart" to "Preorder"..

@cristianfrunza93
Copy link

Any chance you can explain how to add it to the existing code? I have no idea how to add this unfortunately..

I'm trying to change the Add to Cart buttons for Backordered items only - from "Add to Cart" to "Preorder"..

Add that code to Appearance > Theme editor > function.php (at the end)

@cristianfrunza93
Copy link

Worked perfectly, thank you!

@axander44
Copy link

axander44 commented Mar 26, 2021

Hello,
This is the code I put together from everything I found:

`/* backorder text on single product page */

function change_specific_availability_text( $availability ) {

$targeted_text = __( 'Available on backorder', 'woocommerce' );

if ($availability[ 'class' ] == 'available-on-backorder' && $availability[ 'availability' ] == $targeted_text) {

    $availability[ 'availability' ] = __( 'My Custom Text', 'your-theme-textdomain' );

}

return $availability;

}

add_filter( 'woocommerce_get_availability', 'change_specific_availability_text', 20, 1 );

/* Backorder text on cart page */

function alt_message() {
return '

My Custom Text

';
}

function backorder_text($availability) {
$altmessage = alt_message();
foreach($availability as $i) {
$availability = str_replace('Available on backorder', $altmessage, $availability);
}
return $availability;
}
add_filter('woocommerce_get_availability', 'backorder_text');

function woocommerce_custom_cart_item_name( $_product_title, $cart_item, $cart_item_key ) {
$altmessage = alt_message();
if ( $cart_item['data']->backorders_require_notification() && $cart_item['data']->is_on_backorder( $cart_item['quantity'] ) ) {
$_product_title .= __( ' - '. $altmessage, 'woocommerce' ) ;
}
return $_product_title;
}
add_filter( 'woocommerce_cart_item_name', 'woocommerce_custom_cart_item_name', 10, 3);

/* Change Button Name - archive pages */

add_filter( 'woocommerce_get_availability', 'change_specific_availability_text', 20, 1 );

add_filter( 'woocommerce_loop_add_to_cart_link', 'zmena_textu_button_vypis_produktu', 10, 2 );
function zmena_textu_button_vypis_produktu( $text, $product ){
if ( $product->is_on_backorder( 1 ) ) {
$text = __( sprintf(
'<a href="%s" data-quantity="%s" class="%s" %s>%s',
esc_url( $product->add_to_cart_url() ),
esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ),
esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
esc_html( 'Pre-Order' )
), 'woocommerce' );
}
return $text;
}

/* Change Button Name */
add_filter( 'woocommerce_product_single_add_to_cart_text', 'wc_ninja_change_backorder_button', 10, 2 );
function wc_ninja_change_backorder_button( $text, $product ){
if ( $product->is_on_backorder( 1 ) ) {
$text = __( 'Pre-Order', 'woocommerce' );
}
return $text;
}

/* Notification on Cart and Checkout */

function es_add_cart_notice() {
// Only on cart and check out pages
if( ! ( is_cart() || is_checkout() ) ) return;

// Set message
$message = "You have a product with Pre-Order in your Cart.";

// Set variable
$found = false;

// Loop through all products in the Cart        
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
    // Get an instance of the WC_Product object
    $product = $cart_item['data'];

    // Product on backorder
    if( $product->is_on_backorder() ) {
        $found = true;
        break;
    }
}

// true
if ( $found ) {
    wc_add_notice( __( $message, 'woocommerce' ), 'notice' );

    // Removing the proceed button, until the condition is met
    // optional
    // remove_action( 'woocommerce_proceed_to_checkout','woocommerce_button_proceed_to_checkout', 20);
}

}
add_action( 'woocommerce_check_cart_items', 'es_add_cart_notice', 10, 0 );`

I don't like the part for Change Button Name - archive pages .
Can you give the correct code for this.
Also the only thing I want is to add and pre-order text above or below the button on archive pages
1111111

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment