Home Change add to cart is_purchasable notice in Woocommerce
Reply: 1

Change add to cart is_purchasable notice in Woocommerce

Marco Antonio Santanna
1#
Marco Antonio Santanna Published in 2018-01-12 20:02:37Z

I would like to change the notice 'Sorry, this product cannot be purchased' to 'Sorry, the product [product name] cannot be purchased.'

I've found it in the WC_Cart add_to_cart() method source code and below it's an extract of what I would like to change:

if ( ! $product_data->is_purchasable() ) {
   #the current line
   #throw new Exception( __( 'Sorry, this product cannot be purchased.', 'woocommerce' ) ); 
   # i want to replace for this one
   throw new Exception(sprintf( __( 'Sorry, the product "%s" cannot be purchased.', 'woocommerce' ),  $product_data->get_name() ) );
}

Is there a way to do this in a hook or filter or something in my functions.php file?

LoicTheAztec
2#
LoicTheAztec Reply to 2018-01-13 00:04:57Z

This can be done with the following hooked function in WordPress gettext filter hook:

add_filter('gettext', 'renaming_purshasable_notice', 100, 3 );
function renaming_purshasable_notice( $translated_text, $text, $domain ) {
    if( $text === 'Sorry, this product cannot be purchased.' ) {
        $post_title = get_post($GLOBALS['_POST']['add-to-cart'])->post_title;

        $translated_text = sprintf( __( 'Sorry, the product %s cannot be purchased.', $domain ), '"'.$post_title.'"' );
    }
    return $translated_text;
}

Code goes in function.php file of the active child theme (or active theme).

Tested and works.

You will get something like:

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.543349 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO