【发布时间】:2018-06-11 23:24:13
【问题描述】:
我正在使用 Woocommerce 最新版本 3.4.2。如何获取一些订单商品元数据并为其分配自定义值?
- 我使用公共数组
$item_product_data_array获取元数据。 - 我需要获取某个值 - (对产品进行额外修改)。并分配自定义 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