【问题标题】:Woocommerce get specific attribute value on cart pageWoocommerce 在购物车页面上获取特定属性值
【发布时间】:2018-01-11 00:26:25
【问题描述】:

我正在尝试在 woocommerce 购物车页面中获取特定属性值。 我在表格中制作了类似的自定义列

<td class="product-color" data-title="<?php esc_attr_e( 'Color', 'woocommerce' ); ?>">
                        <?php
                            echo apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key );
                        ?>
                    </td>

                    <td class="product-size" data-title="<?php esc_attr_e( 'Size', 'woocommerce' ); ?>">
                        <?php
                            echo apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key );
                        ?>
                    </td>

                    <td class="product-price" data-title="<?php esc_attr_e( 'Price', 'woocommerce' ); ?>">
                        <?php
                            echo apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key );
                        ?>
                    </td>

正如您所见,他们都获得了产品价格。但我正在尝试获取颜色和大小的属性。 我搜索并找到了这个:

foreach( wc_get_product_terms( $product->id, 'pa_size' ) as $attribute_value ){
// Outputting the attibute values one by one
echo $attribute_value . '<br>';

}

我试过这样:

<?php

                            echo apply_filters( 'woocommerce_cart_item_color', WC()->$product->get_attributes( $_product ),
                            $cart_item, $cart_item_key );
                        ?>

但没用

我最近的尝试:

$test = $_product->get_attributes();
foreach($test['pa_size']['options'] as $size){
                            if ($size !== NULL) {
                        echo apply_filters( 'woocommerce_cart_item_price',  $size , $cart_item, $cart_item_key );
                                var_dump($size);

                        }   else {
                                echo "Not Specified";
                            }
                        }

我得到了这个结果 int(48)int(47)

谁能帮帮我。

【问题讨论】:

标签: php wordpress filter woocommerce hook


【解决方案1】:

在 WooCommerce 购物车中显示非常具体的商品属性“color1”

<?php

$item_data = $cart_item['data'];
$attributes = $item_data->get_attributes();

    foreach ( $attributes as $attribute ) 
    {      
        if ( $attribute['name'] == 'colour1' ) {
            $out ='';
            $out .= $attribute['name'] . ': ';
            $out .= $attribute['value'] . '<br />';
            echo $out;
    }
    }
    ?>

由于某些原因get_attributes()崩溃,可以试试

     // For each item in cart
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

    // Get product
    $_product   = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );

    // Get custom attribute
    $foobar = $_product->get_attribute( 'myCustomAttribute' );
    $foobar == true ? $foo = "true" : $foo = "false";  
  }

【讨论】:

    【解决方案2】:

    如何获取购物车页面中的属性和值?

    请在function.php文件中添加以下函数

    <?php
    /**
    * WooCommerce: show all product attributes listed below each item on Cart page 
    * ------
    */    
    
    function wp_woo_cart_attributes( $cart_item, $cart_item_key ) {
    
        $item_data = $cart_item_key['data'];
        $attributes = $item_data->get_attributes();
    
    
        if ( ! $attributes ) {
            return $cart_item;
        }
    
        $out = $cart_item . '<br />';
    
        foreach ( $attributes as $attribute ) {
    
            // skip variations
            if ( $attribute->get_variation() ) {
                continue;
            }
            $name = $attribute->get_name();
            if ( $attribute->is_taxonomy() ) {
    
                $product_id = $item_data->get_id();
                $terms = wp_get_post_terms( $product_id, $name, 'all' );
    
                if ( ! empty( $terms ) ) {
                    if ( ! is_wp_error( $terms ) ) {
    
                        // get the taxonomy
                        $tax = $terms[0]->taxonomy;
    
                        // get the tax object
                        $tax_object = get_taxonomy($tax);
    
                        // get tax label
                        if ( isset ( $tax_object->labels->singular_name ) ) {
                            $tax_label = $tax_object->labels->singular_name;
                        } elseif ( isset( $tax_object->label ) ) {
                            $tax_label = $tax_object->label;
                            // Trim label prefix since WC 3.0
                            $label_prefix = 'Product ';
                            if ( 0 === strpos( $tax_label,  $label_prefix ) ) {
                                $tax_label = substr( $tax_label, strlen( $label_prefix ) );
                            }
                        }
                        $out .= $tax_label . ': ';
                        $tax_terms = array();
                        foreach ( $terms as $term ) {
                            $single_term = esc_html( $term->name );
                            array_push( $tax_terms, $single_term );
                        }
                        $out .= implode(', ', $tax_terms). '<br />';
    
                    }
                }
    
            } else {
    
                // not a taxonomy 
    
                $out .= $name . ': ';
                $out .= esc_html( implode( ', ', $attribute->get_options() ) ) . '<br />';
            }
        }
        echo $out;
    }
    
    add_filter( 'woocommerce_cart_item_name', 'wp_woo_cart_attributes', 10, 2 );
    ?>
    

    【讨论】:

    • 感谢您的重播。但这不是我需要的。我不想在项目下方显示属性。我想将它们显示为表格列。
    猜你喜欢
    • 2020-12-13
    • 2022-08-17
    • 1970-01-01
    • 1970-01-01
    • 2020-07-13
    • 1970-01-01
    • 2021-05-12
    • 1970-01-01
    • 2016-03-30
    相关资源
    最近更新 更多