Reputation: 63
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?
Upvotes: 1
Views: 773
Reputation: 254493
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:
Upvotes: 1