从Woocommerce 3中的订单中获取选定的变体属性 [英] Get the selected variation attributes from orders in Woocommerce 3

查看:73
本文介绍了从Woocommerce 3中的订单中获取选定的变体属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Woocommerce中,我在管理区域中有一个报告,其中列出了所售产品的数量.该站点上仅售出5种产品,但有些产品有1或2种变体.该报告效果很好,但忽略了变化.
我需要从订购的项目中检索属性值,以准确显示数据.
我该怎么做呢?

In Woocommerce, I have a report in the admin area that tallies up products sold.There are only 5 products sold on the site but there are 1 or 2 variations on some. The report works great but ignores the variations.
I need to retrieve the attribute value from items ordered to display the data accurately.
How do I do this?

get_variation_description()不能按我应用的方式工作.

get_variation_description() is not working the way I'm applying it.

我的代码:

$order = wc_get_order( $vs); 

//BEGIN NEW RETRIEVE ORDER ITEMS FROM ORDER 
foreach( $order->get_items() as $item_id => $item_product ){
    $ods = $item_product->get_product_id(); //Get the product ID
    $odqty= $item_product->get_quantity(); //Get the product QTY
    $item_name = $item_product->get_name(); //Get the product NAME
    $item_variation = $item_product->get_variation_description(); //NOT WORKING
}

推荐答案

2020更新-处理自定义产品属性"(重新编码)

2020 Update - Handling "Custom Product Attributes" (revamped code)

WC_Product方法get_variation_description()已过时和不建议使用.它由get_description()方法代替.因此,您需要先获取WC_Product对象.

The WC_Product method get_variation_description() is outdated and deprecated. It's replaced by get_description() method. So you need to get the WC_Product object first.

要获取选定的变体属性,将使用get_variation_attributes( )方法.

// Get an instance of the WC_Order object from an Order ID
 $order = wc_get_order( $order_id ); 

// Loop though order "line items"
foreach( $order->get_items() as $item_id => $item ){
    $product_id   = $item->get_product_id(); //Get the product ID
    $quantity     = $item->get_quantity(); //Get the product QTY
    $product_name = $item->get_name(); //Get the product NAME

     // Get an instance of the WC_Product object (can be a product variation  too)
    $product      = $item->get_product();

     // Get the product description (works for product variation too)
    $description  = $product->get_description();

    // Only for product variation
    if( $product->is_type('variation') ){
         // Get the variation attributes
        $variation_attributes = $product->get_variation_attributes();
        // Loop through each selected attributes
        foreach($variation_attributes as $attribute_taxonomy => $term_slug ){
            // Get product attribute name or taxonomy
            $taxonomy = str_replace('attribute_', '', $attribute_taxonomy );
            // The label name from the product attribute
            $attribute_name = wc_attribute_label( $taxonomy, $product );
            // The term name (or value) from this attribute
            if( taxonomy_exists($taxonomy) ) {
                $attribute_value = get_term_by( 'slug', $term_slug, $taxonomy )->name;
            } else {
                $attribute_value = $term_slug; // For custom product attributes
            }
        }
    }
}

经过测试,可以与其他所有产品类型一起用于产品变体……

Tested and works for a product variation as all other product types…

这篇关于从Woocommerce 3中的订单中获取选定的变体属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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