我目前正在woocommerce上制作一个电子商店。在我的主页上,有一个产品滑块,每个产品下面都有“添加到购物车”按钮。如果我单击该按钮,产品将成功添加到购物车,但没有任何消息。
在网上冲浪时,我发现可以在商店页面、产品类别页面和产品标签页面上添加信息(in this article). 根据那篇文章,我应该使用过滤器/挂钩来捕获添加到购物车事件并在主页上显示消息。
我试过这个:
add_filter( \'woocommerce_add_to_cart_message\', \'custom_add_to_cart_message\' );
function custom_add_to_cart_message() {
global $woocommerce;
// Output success messages
if (get_option(\'woocommerce_cart_redirect_after_add\')==\'yes\') :
$return_to = get_permalink(woocommerce_get_page_id(\'shop\'));
$message = sprintf(\'<a href="%s" class="button">%s</a> %s\', $return_to, __(\'Continue Shopping →\', \'woocommerce\'), __(\'Product successfully added to your cart.\', \'woocommerce\') );
else :
$message = sprintf(\'<a href="%s" class="button">%s</a> %s\', get_permalink(woocommerce_get_page_id(\'cart\')), __(\'View Cart →\', \'woocommerce\'), __(\'Product successfully added to your cart.\', \'woocommerce\') );
endif;
return $message;
}
但什么都没发生。有人能帮我吗?
最合适的回答,由SO网友:Артур Пипченко 整理而成
解决方案非常简单:我刚刚将这段代码添加到我的主页中。php文件:
do_action( \'woocommerce_before_single_product\' );
SO网友:Faisal Ramzan
这篇文章将帮助您解决此问题https://docs.woocommerce.com/document/woocommerce-cart-notices/
也可以在函数中输入以下代码。php获取成功消息
add_filter( \'woocommerce_add_to_cart_message\', \'custom_add_to_cart_message\' );
function custom_add_to_cart_message() {
global $woocommerce;
// Output success messages
if (get_option(\'woocommerce_cart_redirect_after_add\')==\'yes\') :
$return_to = get_permalink(woocommerce_get_page_id(\'shop\'));
$message = sprintf(\'<a href="%s" class="button">%s</a> %s\', $return_to, __(\'Continue Shopping →\', \'woocommerce\'), __(\'Product successfully added to your cart.\', \'woocommerce\') );
else :
$message = sprintf(\'<a href="%s" class="button">%s</a> %s\', get_permalink(woocommerce_get_page_id(\'cart\')), __(\'View Cart →\', \'woocommerce\'), __(\'Product successfully added to your cart.\', \'woocommerce\') );
endif;
return $message;
}
function your_woo_ajax_solution( $translation, $text, $domain ) {
if ( $domain == \'woocommerce\' ) { // your domain name
if ( $text == \'View Cart\' ) { // current text that shows
$translation = \'Product successfully added to your cart.\'; // The text that you would like to show
}
}
return $translation;
}
add_filter( \'gettext\', \'your_woo_ajax_solution\', 10, 3 );