php - 获取订单项目自定义索引并将其分配给 Woocommerce 中的选项值
问题描述
我使用 Woocommerce 最新版本 3.4.2。在这种情况下,我们收集订单数据:产品及其添加剂(我采用元数据)。
如何将变量的索引分配为变量$skus[] = $product->get_sku();
的值$product_mod[] = '';
?
$product_mod[1] = "0";
// 键为 1 的产品(成分糖)是键为 0 的产品修饰符。
// Get product details
$skus = $item_quantities = $line_item_totals = $product_mod = 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();
$product_mod[] = NULL;
$ai = $item->get_meta('Optionally select');
if( strpos( $ai, 'Cinnamon' ) !== false ) {
$skus[] = '10001';
$item_quantities[] ='1';
$line_item_totals[] = '50';
$product_mod[] = '';
}
if( strpos( $ai, 'Sugar' ) !== false ) {
$skus[] = '10002';
$item_quantities[] ='1';
$line_item_totals[] = '50';
$product_mod[] = '';
}
if( strpos( $ai, 'Mint' ) !== false ) {
$skus[] = '10003';
$item_quantities[] ='1';
$line_item_totals[] = '50';
$product_mod[] = '';
}
}
// Product details
foreach ($skus as $key => $value){
$data .= "&product_sku[".$key."]=".$value."";
$data .= "&product_quantity[".$key."]=".$item_quantities[$key]."";
$data .= "&product_price[".$key."]=".$line_item_totals[$key]."";
if( isset($product_mod[$key]) ) {
$data .= "&product_mod[".$key."]=".$key."";
}
}
print_r( $data );
now show: // 为了阅读方便,我写在一个列中,但这是一个字符串。
&product_sku[0]=10030
&product_quantity[0]=1
&product_price[0]=499
&product_sku[1]=10002
&product_quantity[1]=1
&product_price[1]=50
&product_mod[1]=1
需要:
&product_sku[0]=10030 // Coffe sku
&product_quantity[0]=1 // Coffe quantity
&product_price[0]=499 // Coffe price
&product_sku[1]=10002 // Sugar sku
&product_quantity[1]=1 // Sugar quantity
&product_price[1]=50 // Sugar price
&product_mod[1]=0 // Ingredient Sugar with number 1, is a product modifier with number 0.
解决方案
你让事情变得有点复杂了……你需要在一个变量中设置主订单项目索引,以便在选定的附加选项中为你的产品修饰符获取它。无需任何并发症……
我重新审视、简化并压缩了您的代码:
// Array of defined options ( sku => option name )
$options = array(
'10001' => 'Cinnamon',
'10002' => 'Sugar',
'10003' => 'Mint',
);
$count = 0;
// Loop though order items
foreach( $order->get_items() as $item_id => $item){
$product_id = $item->get_product_id();
$product = $item->get_product();
$data .= '&product_sku['.$count.']='.$product->get_sku();
$data .= '&product_quantity['.$count.']='.$item->get_quantity();
$data .= '&product_price['.$count.']='.$item->get_total();
$ind = $count; // Here we set the main order item index in a variable
$count++;
// Get order item selected options
$options_selected = $item->get_meta('Optionally select');
// Loop though order items selected options
foreach( $options as $sku_key => $label_value ){
if( strpos( $options_selected, $label_value ) !== false ) {
$data .= '&product_sku['.$count.']='.$sku_key;
$data .= '&product_quantity['.$count.']=1';
$data .= '&product_price['.$count.']=50';
$data .= '&product_mod['.$count.']='.$ind;
$count++;
}
}
}
// Testing output
print_r( $data );
未经测试,但它应该按预期工作。
推荐阅读
- javascript - Vee-validate 似乎减慢了输入的速度
- docker - Tensorflow Serving - 未找到:操作类型未注册“GatherTree”
- post - 如何使用 API 在 html 中检测该推文是否成功完成?
- apache-spark - 如何使用 yarn rest api 提交作业以激发火花?我想使用 YARN REST API 提交作业以触发
- kubernetes - Kubernetes CronJob - 通知、RunId 和日志
- javascript - 如何在 Laravel Blade 中使用 javascript 输出未转义的 HTML?
- java - 如何处理使用 Selenium Webdriver 的提示警报
- ios - 将 APS 环境设置为开发时,推送通知不起作用
- java - 如何使用 LayoutInflater 动态添加 editText 和按钮并为它们设置唯一的 id?
- sql - 如何强制在 ssrs 报告中显示一周的开始