我整天都在努力寻找如何将HTML内容添加到WooCommerce“添加到购物车”通知消息中。默认情况下,它仅显示纯文本。
仅供参考-实际文本在WooCommerce .po文件中进行了翻译,如下所示:
<h5>added</h5><strong>%s</strong> has been added to your basket.
我已经找到了使用下面的代码片段将输出转换为HTML的方法,但是我无法确定如何将其添加到子主题函数中。php
$message .= "Content-Type: text/html; charset=UTF-8\r\n";
我可以在这种情况下使用它,但更喜欢仅从.po文件中提取文本:
add_filter( 'wc_add_to_cart_message_html', 'custom_add_to_cart_message' );
function custom_add_to_cart_message() {
$message .= "Content-Type: text/html; charset=UTF-8\r\n";
$message = '<h5>added</h5>Product has been added to your basket.' ;
return $message;
}
我的php知识是最少的,我非常感谢您提供一些有关如何实现基本的html更改代码的反馈-add_filter / add_action或类似的东西。
答案 0 :(得分:1)
您可以尝试以下代码:
function custom_add_to_cart_message() {
$return_to = get_permalink(wc_get_page_id('shop'));
if (get_option('woocommerce_cart_redirect_after_add')=='yes') {
$message = sprintf('<a href="%s" class="button">%s</a> %s', get_permalink(wc_get_page_id('cart')), __('View Cart →', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce') );
} else {
$message = sprintf('<a href="%s" class="button">%s</a> %s', $return_to, __('Continue Shopping →', 'woocommerce'), __('Product successfully <h5>added</h5>to your cart.', 'woocommerce') );
}
return $message;
}
add_filter( 'wc_add_to_cart_message', 'custom_add_to_cart_message' );
我检查了它对我来说很好。希望能帮到您。