-
-
Save WPprodigy/b60f63b17bca033ff8753ee058e78b14 to your computer and use it in GitHub Desktop.
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; | |
} |
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"..
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)
Worked perfectly, thank you!
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
This filter for archive pages: https://github.com/woocommerce/woocommerce/blob/02cf0dfaed5923513de0c88add597d1560c2cfd2/includes/abstracts/abstract-wc-product.php#L1864
More info: https://metorik.com/blog/change-the-add-to-cart-text-in-woocommerce