首页 > 解决方案 > 获取订单项目自定义索引并将其分配给 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.

我认为这是正确的方法: 例子

标签: phparrayswordpresswoocommerceorders

解决方案


你让事情变得有点复杂了……你需要在一个变量中设置主订单项目索引,以便在选定的附加选项中为你的产品修饰符获取它。无需任何并发症……

我重新审视、简化并压缩了您的代码:

// 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 );

未经测试,但它应该按预期工作。


推荐阅读