Php 在WooCommerce成功消息中更改呈现的html
在Woocommerce中将产品添加到购物车后,您会收到一条通知,上面写着: “产品”已添加到您的购物车。查看购物车 首先呈现链接元素(视图购物车),其次呈现字符串元素(“产品”已添加到购物车)。我想切换这些元素,以便首先呈现字符串,然后呈现链接 HTML:Php 在WooCommerce成功消息中更改呈现的html,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,在Woocommerce中将产品添加到购物车后,您会收到一条通知,上面写着: “产品”已添加到您的购物车。查看购物车 首先呈现链接元素(视图购物车),其次呈现字符串元素(“产品”已添加到购物车)。我想切换这些元素,以便首先呈现字符串,然后呈现链接 HTML: “快乐忍者”已添加到您的购物车中。 试图追踪代码,但被卡在: 正在查找要添加到functions.php的代码段,请尝试以下操作: add_filter( 'wc_add_to_cart_message_html', function
“快乐忍者”已添加到您的购物车中。
试图追踪代码,但被卡在:
正在查找要添加到functions.php的代码段,请尝试以下操作:
add_filter( 'wc_add_to_cart_message_html', function( $message ) {
return preg_replace(
// Or use ^(<a href=".+?" class="button wc-forward">.+?</a>) *(.+)
'#^(<a href=".+?" class="button wc-forward">.+?</a>) +(.+)#',
'$2 $1',
trim( $message )
);
} );
add_过滤器('wc_add_to_cart_message_html',函数($message){
返回预更换(
//或者使用^()*(.+)
'#^() +(.+)#',
'$2 $1',
修剪($message)
);
} );
如果没有插件(或者主题没有)修改默认HTML标记(如和上所示),那么上面的代码应该可以正常工作
[编辑#3]或者,您可以完全重新生成消息/HTML,如以下示例所示:
add_filter( 'wc_add_to_cart_message_html', 'my_wc_add_to_cart_message_html', 10, 2 );
function my_wc_add_to_cart_message_html( $message, $products ) {
$titles = array();
$count = 0;
foreach ( $products as $product_id => $qty ) {
$titles[] = ( $qty > 1 ? absint( $qty ) . ' × ' : '' ) . sprintf( _x( '“%s”', 'Item name in quotes', 'woocommerce' ), strip_tags( get_the_title( $product_id ) ) );
$count += $qty;
}
$titles = array_filter( $titles );
$added_text = sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', $count, 'woocommerce' ), wc_format_list_of_items( $titles ) );
// Output success messages - the "View cart" or "Continue shopping" link comes after the product link/info.
if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) {
$return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wc_get_raw_referer() ? wp_validate_redirect( wc_get_raw_referer(), false ) : wc_get_page_permalink( 'shop' ) );
$message = sprintf( '%s <a href="%s" class="button wc-forward">%s</a>', esc_html( $added_text ), esc_url( $return_to ), esc_html__( 'Continue shopping', 'woocommerce' ) );
} else {
$message = sprintf( '%s <a href="%s" class="button wc-forward">%s</a>', esc_html( $added_text ), esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View cart', 'woocommerce' ) );
}
return $message;
}
add_过滤器('wc_add_to_cart_message_html','my_wc_add_to_cart_message_html',10,2);
函数my_wc_add_to_cart_message_html($message,$products){
$titles=array();
$count=0;
foreach($product as$product\U id=>$qty){
$titles[]=($qty>1?absint($qty)。×;:“”).sprintf('ux('s&rdquo;'s&rdquo;','Item name in quotes','woocommerce'),strip_标签(获取_title('product_id));
$count+=$qty;
}
$titles=数组过滤器($titles);
$added_text=sprintf(_n('%s已添加到您的购物车中,'%s已添加到您的购物车中,$count,'woocommerce'),wc_格式\u项目列表($titles));
//输出成功消息-“查看购物车”或“继续购物”链接位于产品链接/信息之后。
if('yes'==获取选项('woocommerce\u cart\u redirect\u after\u add')){
$return\u to=apply\u filters('woocommerce\u continue\u shopping\u redirect',wc\u get\u raw\u referer()?wp\u validate\u redirect(wc\u get\u raw\u referer(),false):wc\u get\u page\u permalink('shop');
$message=sprintf('%s',esc_html($added_text),esc_url($return_to),esc_html('Continue shopping','woocommerce');
}否则{
$message=sprintf(“%s”、esc_html($added_text)、esc_url(wc_get_page_permalink('cart'))、esc_html('View cart','woocommerce');
}
返回$message;
}
希望这会有帮助!为什么要这样做?因为视觉上(在WooCommerce 3.3.3版上)“查看购物车”链接显示在通知框的右侧,字符串元素显示在左侧。当然,除非默认外观/CSS是由主题、插件或您修改的。我认为在用户体验方面,最好先有字符串,后有链接。在桌面上工作正常(是的,显示在右侧)但在移动设备中更难做到这一点。对所有设备来说,在之后渲染链接不是更容易吗?好吧,我不太擅长UX,所以我不想回答这个问题。但我有一个PHP代码片段,你可以尝试一下。请!也许是这样:)是的,我们在中添加了一个过滤器。=)太棒了!编辑#3正是我想要的。谢谢!
add_filter( 'wc_add_to_cart_message_html', 'my_wc_add_to_cart_message_html', 10, 2 );
function my_wc_add_to_cart_message_html( $message, $products ) {
$titles = array();
$count = 0;
foreach ( $products as $product_id => $qty ) {
$titles[] = ( $qty > 1 ? absint( $qty ) . ' × ' : '' ) . sprintf( _x( '“%s”', 'Item name in quotes', 'woocommerce' ), strip_tags( get_the_title( $product_id ) ) );
$count += $qty;
}
$titles = array_filter( $titles );
$added_text = sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', $count, 'woocommerce' ), wc_format_list_of_items( $titles ) );
// Output success messages - the "View cart" or "Continue shopping" link comes after the product link/info.
if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) {
$return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wc_get_raw_referer() ? wp_validate_redirect( wc_get_raw_referer(), false ) : wc_get_page_permalink( 'shop' ) );
$message = sprintf( '%s <a href="%s" class="button wc-forward">%s</a>', esc_html( $added_text ), esc_url( $return_to ), esc_html__( 'Continue shopping', 'woocommerce' ) );
} else {
$message = sprintf( '%s <a href="%s" class="button wc-forward">%s</a>', esc_html( $added_text ), esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View cart', 'woocommerce' ) );
}
return $message;
}