alexkodr
alexkodr

Reputation: 543

Change WooCommerce backorder message (product page) based on product category

Im trying to create various different custom backorder messages based on product category for specific products. e.g.

If product is under category X and on backorder display custom backorder message X on product page. If product is under category Y and on backorder display custom backorder message Y on product page. If product is under category Z and on backorder display custom backorder message Z on product page.

I know you can normally hook into filter:

woocommerce_get_availability_text

and do something like below. But this applies to all backorders. Anyone know how i'd go about doing category specific?

function custom_backorder_message( $text, $product ){
  if ( $product->managing_stock() && $product->is_on_backorder( 1 ) ) {
  $text = __( 'Please allow 28 days for delivery of this item' );
}
return $text;
}
add_filter( 'woocommerce_get_availability_text', 'custom_backorder_message', 10, 2 );

Upvotes: 0

Views: 1227

Answers (1)

itzmekhokan
itzmekhokan

Reputation: 2768

Just add follows code snippet -

function custom_backorder_message( $text, $product ){
    if ( $product->managing_stock() && $product->is_on_backorder( 1 ) ) {
        // Dont forget to replace x,y,z with respective categories
        if( has_term( 'x', 'product_cat' ) ) {
            $text = __( 'Please allow 28 days for delivery of this item', 'text-domain' );
        }elseif(has_term( 'y', 'product_cat' ) ){
            $text = __( 'Your Y category product message goes here', 'text-domain' );
        }elseif(has_term( 'z', 'product_cat' ) ){
            $text = __( 'Your Z category product message goes here', 'text-domain' );
        }
    }
    return $text;
}
add_filter( 'woocommerce_get_availability_text', 'custom_backorder_message', 10, 2 );

Upvotes: 1

Related Questions