在Woocommerce 3中获取自定义订单项元数据 [英] Get custom order item metadata in Woocommerce 3

查看:100
本文介绍了在Woocommerce 3中获取自定义订单项元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using Woocommerce latest version 3.4.2. How can I get some order item metadata and assign them a custom value?

  1. 我使用通用数组$item_product_data_array获取元数据.
  2. 我需要获得一定的值-(该产品的其他修改).并分配自定义SKU.
  1. I get meta data with a common array $item_product_data_array.
  2. I need to get a certain value - (Additional modification for the product). And assign custom sku.

示例: 我有咖啡-数组中的sku 1001-键[0] 产品咖啡有其他修饰-糖(元数据) 需求发现糖"并将其分配给自定义sku-50005.

Example: I have coffe - sku 1001, in array - key [0] Product coffe have additional modification - sugar (meta data) Need found "Sugar" and assign him custom sku - 50005.

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

也就是说,此添加剂应以价格或数量为一个周期.他们所有人都必须将其产品的值设为[0].

That is, this additive should be in one cycle as a price or quantity. All of them must treat their product with a value [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]."";
            }
        }

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

I think this will be useful stuff, since all the modules of additional options to the product add all the values to the meta data. And it is difficult to pull them out and include them in the desired cycle.

所以,我们应该得到:

//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开始,您的代码已经过时了……请参阅:

Updated: Your code is really outdated since Woocommerce version 3… See:

  • How to get WooCommerce order details
  • Get Order items and WC_Order_Item_Product in 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().

It should work better… but $product_mod is not defined and $item->get_meta_data() is not used.

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

Now To get some custom meta data, if your custom meta key is Custom thing, you will use:

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

这应该包含在订单项的foreach循环中……经过测试并可以正常工作.

This should be included in the foreach loop of order items… Tested and works.

其他一些事情:

  • 要获得非折扣订单项总计:$item->get_subtotal();
  • 要获取折扣订单项的总计:$item->get_total();
  • 要获取产品价格(单位):$product->get-price();
  • To get the NON discounted order line item total: $item->get_subtotal();
  • To get the discounted order line item total: $item->get_total();
  • To get the product price (unit): $product->get-price();

这篇关于在Woocommerce 3中获取自定义订单项元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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