【问题标题】:Get custom order item metadata in Woocommerce 3在 Woocommerce 3 中获取自定义订单项目元数据
【发布时间】:2018-06-11 23:24:13
【问题描述】:

我正在使用 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

【问题讨论】:

  • @LoicTheAztec 最新版本。块“//产品详细信息”效果很好。
  • 谢谢!请帮助修改产品和分配 sku。对产品的附加修改总是数量 = 1。
  • 各位大佬,能不能告诉我,应该采取什么样的措施?我会尝试自己编写代码。目前我不知道从哪里开始。
  • 如果我在一般周期中找到正确的成分 -> 我给它值'50005' -> 我给 $item_quantities[] = 1

标签: php wordpress woocommerce metadata orders


【解决方案1】:

更新:自 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();

【讨论】:

  • 请告诉我,在这种情况下 _custom_thing - $custom_thing = $product->get_meta( '_Sugar' );?理论上,我需要在元数据中找到所有的添加剂,然后给他们我的 sku。它们的数量默认为 1。
  • 谢谢!唯一遗憾的是不清楚如何实现预期的任务。也许我写的有点不清楚我想要实现的目标......部分$product_mod
猜你喜欢
  • 2018-07-11
  • 1970-01-01
  • 2018-12-07
  • 2020-12-10
  • 2021-07-13
  • 2018-07-12
  • 1970-01-01
  • 2014-08-22
  • 1970-01-01
相关资源
最近更新 更多