Woocommerce,如何编辑"添加到购物车"消息

Dan*_*nte 8 wordpress hook edit e-commerce woocommerce

当点击添加到购物车按钮时,Woocommerce显示消息,查看购物车,我想编辑此消息,实际编辑所有跨度,放一些图标等...

zmo*_*eca 16

在theme/functions.php中添加一个过滤器.下面的代码只是覆盖了现有的$ message.这将覆盖$ message,其中包含一个与消息"checkout"链接相同的消息.

确保您返回$消息.

您当然可以修改现有消息,因为整个事物通过第一个参数或$ message var作为字符串传递.

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 added to your cart.', '%s have been added to your cart.', sizeof( $titles ), 'woocommerce' ), wc_format_list_of_items( $titles ) );

    $message = sprintf( '%s <a href="%s" class="button">%s</a>&nbsp;<a href="%s" class="button">%s</a>',
                    esc_html( $added_text ),
                    esc_url( wc_get_page_permalink( 'checkout' ) ),
                    esc_html__( 'Checkout', 'woocommerce' ),
                    esc_url( wc_get_page_permalink( 'cart' ) ),
                    esc_html__( 'View Cart', 'woocommerce' ));

    return $message;
}
Run Code Online (Sandbox Code Playgroud)

  • 该解决方案不再起作用。WC 3.0要求使用“ wc_add_to_cart_message_html”而不是“ wc_add_to_cart_message”和“ wc_add_to_cart_message_filter” (3认同)

Bra*_*eyD 6

您是否尝试过以下过滤器

function your_add_to_cart_message() {
if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) :
    $message = sprintf( '%s<a href="%s" class="your-style">%s</a>', __( 'Successfully added to cart.', 'woocommerce' ), esc_url( get_permalink( woocommerce_get_page_id( 'shop' ) ) ), __( 'Continue Shopping', 'woocommerce' ) );
else :
    $message = sprintf( '%s<a href="%s" class="your-class">%s</a>', __( 'Successfully added to cart.' , 'woocommerce' ), esc_url( get_permalink( woocommerce_get_page_id( 'cart' ) ) ), __( 'View Cart', 'woocommerce' ) );
endif;
return $message;
}
add_filter( 'wc_add_to_cart_message', 'your_add_to_cart_message' );
Run Code Online (Sandbox Code Playgroud)

要回复ajax消息更新,请尝试翻译功能,例如:

function your_woo_ajax_solution( $translation, $text, $domain ) {
  if ( $domain == 'woocommerce' ) { // your domain name
    if ( $text == 'View Cart' ) { // current text that shows
        $translation = 'Basket updated.'; // The text that you would like to show
    }
  }

  return $translation;
}
add_filter( 'gettext', 'your_woo_ajax_solution', 10, 3 );
Run Code Online (Sandbox Code Playgroud)


Loi*_*tec 6

\n

2017 - 2019 - 对于 Woocommerce 3+ (处理添加到购物车的多个产品)

\n
\n\n

替换为wc_add_to_cart_message_html过滤器挂钩,第二个函数参数已更改为$products (而不是$product_id \xe2\x80\xa6

\n\n

您可以对此挂钩函数内的代码进行更改,例如在此线程中:

\n\n
add_filter( \'wc_add_to_cart_message_html\', \'custom_add_to_cart_message_html\', 10, 2 );\nfunction custom_add_to_cart_message_html( $message, $products ) {\n    $titles = array();\n    $count  = 0;\n\n    foreach ( $products as $product_id => $qty ) {\n        $titles[] = ( $qty > 1 ? absint( $qty ) . \' &times; \' : \'\' ) . sprintf( _x( \'&ldquo;%s&rdquo;\', \'Item name in quotes\', \'woocommerce\' ), strip_tags( get_the_title( $product_id ) ) );\n        $count += $qty;\n    }\n\n    $titles     = array_filter( $titles );\n    $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 ) );\n\n    // The custom message is just below\n    $added_text = sprintf( _n("%s item has %s", "%s items have %s", $count, "woocommerce" ),\n        $count, __("been added to your basket.", "woocommerce") );\n\n    // Output success messages\n    if ( \'yes\' === get_option( \'woocommerce_cart_redirect_after_add\' ) ) {\n        $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\' ) );\n        $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 ) );\n    } else {\n        $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 ) );\n    }\n    return $message;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

相关主题(适用于 Woocommerce 3+):

\n\n\n