简体   繁体   English

Woocommerce 在购物车和结帐时打印简单的产品属性,如变量属性

[英]Woocommerce print simple product attributes on cart and checkout, like variable attributes

I want to show attibutes from a simple product, in cart and checkout.我想在购物车和结帐中显示一个简单产品的属性。 Just like variable products does, but there is only one attibute.就像可变产品一样,但只有一种属性。 See image below:见下图:

简单的产品属性演练

Is this possible to achive with PHP?这是否可以通过 PHP 实现?

I was thinking about something like using echo $product->get_attributes()我正在考虑使用 echo $product->get_attributes()

Yes you can.是的你可以。 Please try adding a filter as bellow,请尝试添加过滤器,如下所示,

add_filter('woocommerce_cart_item_name', function($name, $cart_item) {
    //has attributes
    if ($cart_item['data']->is_type( 'simple' ) && $attributes = $cart_item['data']->get_attributes()) {
        $name .= " - ";
        foreach ($attributes as $att)
            $name .= $att->get_name() . " : " . implode(',', $att->get_options());
    }
    return $name;
}, 10, 2);

Add the follows code snippets to achieve your above task -添加以下代码片段以实现上述任务 -

function modify_woocommerce_get_item_data( $item_data, $cart_item ) {
    if( $item_data || $cart_item['data']->is_type( 'variation' ) ) return $item_data;
    if ( $cart_item['data']->is_type( 'simple' ) ) {
    $attributes = array_filter( $cart_item['data']->get_attributes(), 'wc_attributes_array_filter_visible' );

    foreach ( $attributes as $attribute ) {
        $values = array();

            if ( $attribute->is_taxonomy() ) {
                $attribute_taxonomy = $attribute->get_taxonomy_object();
                $attribute_values   = wc_get_product_terms( $cart_item['data']->get_id(), $attribute->get_name(), array( 'fields' => 'all' ) );

                foreach ( $attribute_values as $attribute_value ) {
                    $value_name = esc_html( $attribute_value->name );

                    if ( $attribute_taxonomy->attribute_public ) {
                        $values[] = '<a href="' . esc_url( get_term_link( $attribute_value->term_id, $attribute->get_name() ) ) . '" rel="tag">' . $value_name . '</a>';
                    } else {
                        $values[] = $value_name;
                    }
                }
            } else {
                $values = $attribute->get_options();

                foreach ( $values as &$value ) {
                    $value = make_clickable( esc_html( $value ) );
                }
            }

            $item_data[] = array(
                'key'   => wc_attribute_label( $attribute->get_name() ),
                'value' => apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values ),
            );
        }
    }
    return $item_data;
}
add_filter( 'woocommerce_get_item_data', 'modify_woocommerce_get_item_data', 99, 2 );

Codes goes to your active theme's functions.php代码转到您的活动主题的功能。php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WooCommerce 有条件的简单或可变产品和属性 - WooCommerce Conditional for simple or variable product and attributes 在 WooCommerce 中将可变产品的自定义属性添加到购物车 - Add Custom attributes from a variable product to cart in WooCommerce 在事件checkout_cart_product_add_after中获取产品属性 - Getting product attributes in event checkout_cart_product_add_after 具有层次结构的 Woocommerce 产品属性,如类别 - Woocommerce product attributes with hierarchy like categories 在Woocommerce购物车/结帐中将变体属性值显示为单独的行 - Display variations attributes values as separate rows in Woocommerce cart / checkout 结帐前需要 WooCommerce 购物车中的简单产品以进行产品变体 - Require simple products in WooCommerce cart for product variations before checkout 在 Woocommerce 档案中显示可变产品属性和术语 - Display Variable Product Attributes and Terms on Woocommerce Archives Woocommerce以编程方式创建可变产品的多个属性 - Woocommerce create variable product multiple attributes programmatically 在WooCommerce购物车中回应特定的产品属性和元数据 - Echo specific Product Attributes and Meta Data in WooCommerce Cart 在 Woocommerce 中显示添加到购物车消息的产品变体属性 - Show product variation attributes on added to cart message in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM