在Woocommerce中显示添加到购物车消息中的产品变体属性

问题描述 投票:1回答:2

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

scamp of return

php wordpress woocommerce custom-taxonomy notice
2个回答
1
投票

是,启用调试后,代码会引发一些错误:

Notice:未定义索引:../ wp-content / themes / storefront-child / functions.php中xxxx行上的attribute_size

基本上是这行:

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

似乎此代码是为了处理“大小”自定义属性,而不是任何属性。

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

enter image description here

为了使其工作并显示带有其术语名称值的正确属性标签名称,请使用以下重新审阅的代码版本:

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文件。经过测试,可以正常工作。

enter image description here


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

替换此行代码:

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

此人:

$variation_attributes[] = $term_name;

您将得到类似的东西:

enter image description here


0
投票

非常感谢!很棒!但是将产品添加到购物车时出现问题,该产品不再可用。无法检出,因为出现消息说该文章不可用。但是,在此消息中,未指定product属性。是否可以向该消息添加属性?

© www.soinside.com 2019 - 2024. All rights reserved.