在 Woocommerce 3 中获取自定义订单项元数据

Get custom order item metadata in Woocommerce 3

我正在使用 Woocommerce 最新版本 3.4.2。如何获取一些订单项元数据并为其分配自定义值?

  1. 我使用通用数组 $item_product_data_array 获取元数据。
  2. 我需要获得一定的价值-(对产品的额外修改)。并分配自定义 sku。

示例: 我有咖啡 - sku 1001,数组 - 键 [0] 产品咖啡有额外的修改 - 糖(元数据) 需要找到 "Sugar" 并为其分配自定义 sku - 50005。

[label] => Optionally select [value] => Array ( [0] => Cinnamon [1] => Sugar [2] => Mint )

也就是这个加法应该在一个周期内作为一个价格或者数量。他们所有人都必须用值 [0].

来对待他们的产品
    // Get product details
        $items = $order->get_items();

        $sku = array();
        $product_kol = array();
        $product_price = array();
        $item_product_meta_data_array = array();

        foreach( $items as $key => $item){
            $product_kol[] = $item['qty'];
            $product_price[] = $item['line_total'];

            $item_id = $item['product_id'];
            $product = wc_get_product($item_id);
            $sku[] = $product->get_sku();

            $items_meta_data[]  = $item->get_meta_data();          
            $meta_data1 = $item->get_meta('Sugar');
                if( ! empty( $meta_data1 ) ) {
                $skus[] = "50005";
                $item_quantities[] = "1";
            }
        }

        // Product details for sending as one line to an external service
        foreach ($sku as $key => $value){
            $data .= "&product[".$key."]=".$value."";
            $data .= "&product_kol[".$key."]=".$product_kol[$key]."";
            $data .= "&product_price[".$key."]=".$product_price[$key]."";
            if(isset($product_mod[$key])) {
                $data .= "&product_mod[".$key."]=".$product_mod[$key]."";
            }
        }

我认为这将是有用的东西,因为产品附加选项的所有模块都会将所有值添加到元数据中。并且很难将它们拉出来并包含在所需的周期中。

所以,我们应该得到:

//products sku 
$product[0] = "10000";  // Coffe
$product[1] = "10001";  // Juice - second product for axample
$product[2] = "50005";  // Sugar

//products quantity
$product_kol[0] = "1"; // Аmount of coffee
$product_kol[1] = "1"; // Аmount of juice
$product_kol[2] = "1"; // Аmount of sugar

//Modifiers if they exist
$product_mod[2] = "0";  //The product with key 2 is the product modifier with key 0

更新: 自 Woocommerce 版本 3 以来,您的代码确实已经过时了……参见:

所以你的代码应该是:

$skus = $item_quantities = $line_item_totals = $items_meta_data = array();

// Loop though order items
foreach( $order->get_items() as $item_id => $item){
    $product_id = $item->get_product_id();
    $product = $item->get_product();

    $item_quantities[] = $item->get_quantity();
    $line_item_totals[] = $item->get_total();
    $skus[]            = $product->get_sku();
    $items_meta_data[]  = $item->get_meta_data();
}

// Product details for sending as one line to an external service
foreach ($skus as $key => $value){
    $data .= "&product[".$key."]=".$value."";
    $data .= "& product_kol[".$key."]=".$item_quantities[$key]."";
    $data .= "& product_price[".$key."]=".$line_item_totals[$key]."";
    if( isset($product_mod[$key]) ) {
        $data .= "&product_mod[".$key."]=".$product_mod[$key]."";
    }
}

它应该工作得更好......但是 $product_mod 没有定义并且 $item->get_meta_data() 没有被使用。


现在要获取一些自定义元数据,如果您的自定义元键Custom thing,您将使用:

 $custom_thing = $item->get_meta('Custom thing');

这应该包含在订单项的 foreach 循环中……经过测试并且有效。


其他一些事情:

  • 获取非折扣订单行项目总计:$item->get_subtotal();
  • 获取折扣订单行项目总计:$item->get_total();
  • 获取商品价格(单位):$product->get-price();