显示woocommerce中的产品变化已添加到购物车消息 [英] Show product variation in woocommerce Added to cart message

查看:100
本文介绍了显示woocommerce中的产品变化已添加到购物车消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的WooCommerce商店中,将可变产品添加到购物车后,我让用户停留在产品页面上.

In my WooCommerce store, when a variable product is added to cart I have the user stay on the product page.

成功通知消息显示产品标题以及通用附件,但我希望它在通知中显示产品标题的变体,如下所示:

The success notice message shows the product title along with the generic appendage, but I want it to show the variations with the product title in notices like so:

男士外套-尺寸:小"已添加到您的购物车.

"Mens Coat - size: small" has been added to your cart.

而不是:

男士外套"已添加到您的购物车.

"Mens Coat" has been added to your cart.

当前正在运行WooCommerce 3.3.1和WP 4.9.

Currently running WooCommerce 3.3.1 and WP 4.9.

谢谢

推荐答案

此代码是动态的,因此可用于添加到可变产品中的任意数量的属性.它将照常处理简单的产品.

This code is dynamic so will work for any number of attributes added to a variable product. It will handle simple product as per usual.

function modify_wc_add_to_cart_message( $message, $products ) {
    $attribute_label = '';
    $titles = array();
    $count  = 0;

    foreach ( $products as $product_id => $qty ) {
        $product = wc_get_product( $product_id );

        if( $product->is_type( 'variable' ) ) {
            foreach( $product->get_variation_attributes() as $attribute_name => $attribute_values ) {
                if( isset( $_REQUEST['attribute_' . strtolower( $attribute_name )] ) ) {
                    if( in_array( $_REQUEST['attribute_' . strtolower( $attribute_name )], $attribute_values ) ) {
                        if( ! empty( $attribute_label ) )
                            $attribute_label .= ', ';

                        $attribute_label .= $attribute_name . ' : ' . $_REQUEST['attribute_size'];
                    }
                }
            }
        }

        $titles[] = ( $qty > 1 ? absint( $qty ) . ' × ' : '' ) . sprintf( _x( '“%s”', 'Item name in quotes', 'woocommerce' ), strip_tags( get_the_title( $product_id ) ) . ( ! empty( $attribute_label ) ? ' - ' . $attribute_label : '' ) ) ;
        $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 ) );

    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;
}

add_filter( 'wc_add_to_cart_message_html', 'modify_wc_add_to_cart_message', 10, 2 );

这篇关于显示woocommerce中的产品变化已添加到购物车消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆