在Woocommerce中更改添加到购物车is_purchasable通知

时间:2018-01-12 20:02:37

标签: php wordpress woocommerce gettext product

我想更改通知'抱歉,此产品无法购买'到'对不起,产品[产品名称]无法购买。'

我在WC_Cart add_to_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文件中的钩子或过滤器或其他内容中执行此操作?

1 个答案:

答案 0 :(得分:1)

这可以通过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文件。

经过测试和工作。

你会得到类似的东西:

enter image description here