【问题标题】:Get the selected variation attributes from orders in Woocommerce 3从 Woocommerce 3 中的订单中获取选定的变体属性
【发布时间】:2018-01-22 21:50:35
【问题描述】:

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

get_variation_description() 无法按照我的应用方式工作。

我的代码:

$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
}

【问题讨论】:

    标签: php wordpress woocommerce product variations


    【解决方案1】:

    2020 年更新 - 处理“自定义产品属性”(修改代码)

    WC_Product 方法get_variation_description() 已过时且已弃用。它被get_description() 方法取代。所以需要先获取WC_Product对象。

    要获取选定的变体属性,您将使用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
                }
            }
        }
    }
    

    与所有其他产品类型一样,经过测试并适用于产品变体……

    【讨论】:

    • 谢谢,但我无法通过这种方式检索客户选择的属性值。当你到达 $variation_attributes 并对其进行 var_dump 时,我的输出是 array(1) { ["attribute_dish-options"]=> string(0) "" }。当您通过循环时,$attribute_name 和 $attribute_value 都为空。这是因为我们得到的是 WC_Product 的实例而不是 WC_Order 对象吗?
    • 使用您的代码帮助我识别属性名称分类法。我在下面的代码中使用了它,它现在返回选定的属性值。有没有办法遍历下面的代码以返回所有选定的属性值(如果每个产品有多个变体),而不必在代码中命名每个属性名称? $dish_options = wc_get_order_item_meta( $item_id, "dish-options", true );这是您之前在stackoverflow.com/questions/44985868/…的答案之一
    • @ComputerGiant 这个答案,只是回答你最初的问题。我的代码经过测试并在 WC 3+ 中适用于所有类型的产品变体,具有一个或多个属性/值对(自定义或非自定义)。你不需要命名任何东西,你只需要获取和操作你需要的数据......评论不是很实用,我并没有真正抓住上面你想要的东西。如果您还有其他问题,最好提出一个新问题(如果您愿意,请在此处通知我)。谢谢。
    • 好的,我会再测试一次,看看我能不能说出为什么我没有得到预期的结果。
    • @Tom 更新了我的答案代码以处理“自定义产品属性”...
    【解决方案2】:

    完美显示订单商品名称和属性键

    foreach( $order->get_items() as $order_item_product ) {
        $item_meta_data = $order_item_product->get_meta_data();
        echo $product_name = $order_item_product->get_name();
        echo '<br>';
        foreach( $item_meta_data as $meta_data ) {
            $meta_data_as_array = $meta_data->get_data();
            echo $meta_data_as_array['key'].': '.$meta_data_as_array['value'].'<br>';
        }
    }
    

    【讨论】:

    • 获取所有订单产品元数据的可靠、简单的答案
    【解决方案3】:

    基于接受的答案这是为了纠正错字(我没有做其他任何事情的声誉)。 注意 $attribute_value 属性定义上的 $term_slug。这就是缺少 $ 的原因。

    // 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 ){
          $product_id = $item_product->get_product_id(); //Get the product ID
          $quantity = $item_product->get_quantity(); //Get the product QTY
          $product_name = $item_product->get_name(); //Get the product NAME
    
          // Get an instance of the WC_Product object (can be a product variation  too)
          $product = $item_product->get_product();
    
          // Get the product description (works for product variation)
          $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){
                $taxonomy = str_replace('attribute_', '', $attribute_taxonomy );
                // The name of the attribute
                $attribute_name = get_taxonomy( $taxonomy )->labels->singular_name;
                // The term name (or value) for this attribute
                $attribute_value = get_term_by( 'slug', $term_slug, $taxonomy )->name;
            }
          }
       }
    

    【讨论】:

    • 伟大的收获!谢谢
    猜你喜欢
    • 2019-11-12
    • 1970-01-01
    • 1970-01-01
    • 2018-03-17
    • 2020-01-11
    • 2018-09-15
    • 2020-01-25
    • 1970-01-01
    • 2018-11-24
    相关资源
    最近更新 更多