Last active
December 4, 2022 16:17
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello,
This is the code I put together from everything I found:
`/* backorder text on single product page */
function change_specific_availability_text( $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;
}
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