从Woocommerce 3中的订单中获取所选的变体属性

时间:2018-01-22 21:50:36

标签: php wordpress woocommerce product variations

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

3 个答案:

答案 0 :(得分:1)

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 ){
    $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;
        }
    }
}

测试并适用于所有其他产品类型的产品变体......

答案 1 :(得分:1)

基于接受的答案这是为了可以纠正错字(我没有做任何其他事情的名声)。 注意$ 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;
        }
      }
   }

答案 2 :(得分:0)

它非常适合显示订单商品名称和属性键

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>';
    }
}