Reputation: 1
I was able to find some code on here to change the backorder message on WooCommerce. It worked really well. I then decided to use ACF to make it so I could have a custom message per product. This also worked well on products with no variations. On products with variations, it repeated this message for as many times as there are variations. So, if there are 3 variations, the message is repeated three times. Is there a way to make it only display once per product?
This is the code that I'm using:
function alt_message() {
return the_field('backorder_message');
}
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);
Upvotes: 0
Views: 33