Php 更改添加到购物车是Woocommerce中可购买的通知

Php 更改添加到购物车是Woocommerce中可购买的通知,php,wordpress,woocommerce,gettext,product,Php,Wordpress,Woocommerce,Gettext,Product,我想将通知“抱歉,此产品无法购买”更改为“抱歉,产品[产品名称]无法购买” 我在WC\u Cartadd\u to\u Cart()方法源代码中找到了它,下面是我想要更改的内容的摘录: if ( ! $product_data->is_purchasable() ) { #the current line #throw new Exception( __( 'Sorry, this product cannot be purchased.', 'woocommerce' ) )

我想将通知“抱歉,此产品无法购买”更改为“抱歉,产品[产品名称]无法购买”

我在
WC\u Cart
add\u to\u Cart()
方法源代码中找到了它,下面是我想要更改的内容的摘录:

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() ) );
}

在我的
functions.php
文件中的钩子、过滤器或其他东西中是否有这样做的方法?

这可以通过WordPress
gettext
过滤器钩子中的以下钩子函数来完成:

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;
}
代码进入活动子主题(或活动主题)的function.php文件

测试和工作

您将得到如下结果:


它似乎工作得很好,但我想我遗漏了一些信息,当我再次使用订单时,此消息可能会出现,并且产品不存在或是草稿。因此,它没有显示产品名称,而是显示“Twitter”@Marcantoniosantanna谢谢…如果我发现在这篇评论中解释的特定案例对你有用,我会给你发消息。谢谢@LoicTheAztecI有类似的问题,有人可以看看这里吗