【问题标题】:Show product variation attributes on added to cart message in Woocommerce在 Woocommerce 中显示添加到购物车消息的产品变体属性
【发布时间】:2018-11-17 09:14:52
【问题描述】:

在 WooCommerce 中,当有人将产品添加到购物篮时,会显示一条通知消息以进行确认。我希望能够显示客户放入购物篮的变化,而不是显示父级。

我正在使用来自Show product variation in woocommerce Added to cart message 的答案中的 Andrew Schultz 的代码,我猜钩子或名称已经改变,因为正在发生的事情是“pa_subject”在它应该说“地理”时显示,例如。

任何想法如何改变它?

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

【问题讨论】:

    标签: php wordpress woocommerce custom-taxonomy notice


    【解决方案1】:

    是的,启用调试时代码会抛出一些错误:

    注意:未定义索引:../wp-content/themes/storefront-child/functions.php 第 xxxx 行中的属性大小

    基本上就是这一行:

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

    似乎这段代码是用来处理“大小”自定义属性的,而不是任何属性。

    它还显示以“pa_”开头的 woocommerce 属性 slug,而不是属性分类标签名称:

    因此,要使其工作并显示正确的属性标签名称及其术语名称值,请使用以下重新访问的代码版本:

    add_filter( 'wc_add_to_cart_message_html', 'change_add_to_cart_message', 10, 2 );
    function change_add_to_cart_message( $message, $products ) {
        $titles = array();
        $count  = 0;
    
        foreach ( $products as $product_id => $qty ) {
            // Get the WC_Product object instance
            $product = wc_get_product( $product_id );
            if( $product->get_type() === 'variable' ) {
                $variation_attributes = array();
                foreach( $product->get_variation_attributes() as $taxonomy => $terms_slugs ) {
                    $wc_attribute_name = wc_variation_attribute_name( $taxonomy );
                    if( isset( $_REQUEST[$wc_attribute_name] ) ) {
                        if( in_array( $_REQUEST[$wc_attribute_name], $terms_slugs ) ) {
                            $term_name = get_term_by( 'slug', $_REQUEST[$wc_attribute_name], $taxonomy )->name;
                            $variation_attributes[] = wc_attribute_label( $taxonomy ) . ': ' . $term_name;
                        }
                    }
                }
                $variation_attributes = implode(', ', $variation_attributes);
            }
    
            $titles[] = ( $qty > 1 ? absint( $qty ) . ' &times; ' : '' ) . sprintf( _x( '&ldquo;%s&rdquo;', 'Item name in quotes', 'woocommerce' ), strip_tags( get_the_title( $product_id ) ) . ( ! empty( $variation_attributes ) ? ' - ' . $variation_attributes : '' ) ) ;
            $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;
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。


    仅显示不带分类标签名称的术语名称

    在代码中替换这一行:

    $variation_attributes[] = wc_attribute_label( $taxonomy ) . ': ' . $term_name;
    

    这个:

    $variation_attributes[] = $term_name;
    

    你会得到类似的东西:

    【讨论】:

    • 你是绝对的明星!这工作得很好。谢谢@loictheaztec
    猜你喜欢
    • 2018-08-01
    • 2018-09-05
    • 1970-01-01
    • 1970-01-01
    • 2012-12-04
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    • 1970-01-01
    相关资源
    最近更新 更多