首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Woocommerce 3中自定义添加到购物车消息

在Woocommerce 3中自定义添加到购物车消息
EN

Stack Overflow用户
提问于 2018-12-18 17:11:42
回答 1查看 7.8K关注 0票数 1

我想更改“产品名称”已添加到您的篮子‘提示说'x项目(S)已经/已经添加到您的篮子’。

This thread解释了如何编辑add to cart消息,但是我找不到可以使用的变量的任何信息。

如何显示添加的产品数量而不是名称?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-18 20:05:26

自从Woocommerce 3之后,过滤器钩子wc_add_to_cart_message就过时了,现在它被wc_add_to_cart_message_html…取代了

函数可用变量参数有两个:

  • $message,是输出的字符串消息。
  • $products,它是一个索引数组,包含产品Id /数量对(key / value)。

若要更改默认的"__{qty} x ,请将(Ve)添加到购物车“消息”中:

{qty} 项已/已添加到您的篮中。

您将使用以下内容:

代码语言:javascript
运行
复制
add_filter( 'wc_add_to_cart_message_html', 'custom_add_to_cart_message_html', 10, 2 );
function custom_add_to_cart_message_html( $message, $products ) {
    $count = 0;
    foreach ( $products as $product_id => $qty ) {
        $count += $qty;
    }
    // The custom message is just below
    $added_text = sprintf( _n("%s item has %s", "%s items have %s", $count, "woocommerce" ),
        $count, __("been added to your basket.", "woocommerce") );

    // Output success messages
    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( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( $return_to ), esc_html__( 'Continue shopping', 'woocommerce' ), esc_html( $added_text ) );
    } else {
        $message   = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View cart', 'woocommerce' ), esc_html( $added_text ) );
    }
    return $message;
}

代码在您的活动子主题(或活动主题)的function.php文件中。测试和工作。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53837991

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档