【发布时间】:2017-08-19 08:26:55
【问题描述】:
我的网站上有两种产品,并且想在将不同产品添加到购物车时显示不同的消息(在消息中我想使用 HTML)。现在它显示Product successfully added to cart.
我在我孩子的 function.php 中使用了这段代码,它正在工作,但没有给我我真正想要的。
add_filter ( 'wc_add_to_cart_message', 'wc_add_to_cart_message_filter', 10, 2 );
function wc_add_to_cart_message_filter($message, $product_id = null) {
$titles[] = get_the_title( $product_id );
$titles = array_filter( $titles );
$added_text = sprintf( _n( '%s has been successfully added to your Basket.', '%s have been added to your Basket.', sizeof( $titles ), 'woocommerce' ), wc_format_list_of_items( $titles ) );
$message = sprintf( '%s <a href="%s" class="button">%s</a>',
esc_html( $added_text ),
esc_url( wc_get_page_permalink( 'cart' ) ),
esc_html__( 'View Cart', 'woocommerce' ));
return $message;
}
【问题讨论】:
-
我有两个产品,分别是 p1 和 p2,如果其中一个产品被添加到购物车,它会显示一条消息“您的产品已添加到购物车和一个‘查看购物车’按钮”。我想在添加不同产品时显示不同的消息,例如如果我将 p1 添加到购物车它应该显示“message1”,如果将 p2 添加到购物车它应该显示“message2”。我希望现在它是有意义的。
标签: php wordpress woocommerce product messages