从 WooCommerce 3 中的订单项中获取一组特定数据 [英] Get an array of specific data from order items in WooCommerce 3

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

问题描述

我需要在 json 中得到以下响应:

I need the following response in a json:

    $output = array(
        'storeCode' => '123456',
        'firstName' => $fname,
        'lastName' => $lname,
        'dateOfBirth' => '1983-04-01',
        'mobilePhone' => $mobile,
        'email' => $email,
        'address' => array(
        'street' => $address1,
        'suiteApartment' => $address2,
        'city' => $city,
        'state' => $state,
        'zipCode'=> $zip
        ), 
        'products' => $orderitems,
        'redirectUrl' => $cburl,
        'referenceNumber' => $order_id
    );

当我尝试以下操作时,我只能获得 $orderitems 的第一个实例:

I can only get the first instance of $orderitems when I try the following:

    $order = wc_get_order($order_id);

    // Get the order ID
    $order_id = $order->get_id();

    // Initialising
    $items = $order->get_items();
    $count = 0;

    // Loop through Order items
    foreach ($order->get_items() as $item_key => $item ):
        $product      = $item->get_product(); // Get the WC_Product object

        $product_id   = $item->get_product_id(); // the Product id
        $variation_id = $item->get_variation_id(); // the Variation id

        $item_type    = $item->get_type(); // Type of the order item ("line_item")

        $item_name    = $item->get_name(); // Name of the product
        $quantity     = $item->get_quantity();  
        $tax_class    = $item->get_tax_class();
        $line_subtotal     = $item->get_subtotal(); // Line subtotal (non discounted)
        $line_subtotal_tax = $item->get_subtotal_tax(); // Line subtotal tax (non discounted)
        $line_total        = $item->get_total(); // Line total (discounted)
        $line_total_tax    = $item->get_total_tax(); // Line total tax (discounted)

        $orderitems = array(
            'id' => $product_id,
            'description' => $item_name,
            'quantity' => $quantity,
            'value' => $line_subtotal,
            'salesTax' => $line_subtotal_tax
        );
        $count++; 
    endforeach;

为什么在回显变量时我只得到数组中的第一项?

Why am I only getting the first item in the array when echo out the variable?

推荐答案

您的代码中存在一些小错误...尝试以下操作,您将获得变量中所有订单商品的特定数据,以用于您的 'products' 参数:

There is some little mistakes in your code… Try the following where you will get the specific data for all order items in your variable to be used on your 'products' argument:

$order_items = $order->get_items(); // Get order items array of objects
$items_count = count($items); // Get order items count
$items_data  = []; // Initializing

// Loop through order items
foreach ( $order_items as $item_id => $item ) {
    $variation_id = item->get_variation_id();
    $product_id   = $variation_id > 0 ? $variation_id : $item->get_product_id();

    // Set specific data for each item in the array
    $items_data[] = array(
        'id'          => $product_id,
        'description' => $item->get_name(),
        'quantity'    => $item->get_quantity(),
        'value'       => $item->get_subtotal(),
        'salesTax'    => $item->get_subtotal_tax(),
    );
}

然后这将用于:

$output = array(
    'storeCode'       => '123456',
    'firstName'       => $order->get_billing_first_name(),
    'lastName'        => $order->get_billing_last_name(),
    'dateOfBirth'     => '1983-04-01',
    'mobilePhone'     => $order->get_billing_phone(),
    'email'           => $order->get_billing_email(),
    'address'         => array(
        'street'          => $order->get_billing_address_1(),
        'suiteApartment'  => $order->get_billing_address_2(),
        'city'            => $order->get_billing_city(),
        'state'           => $order->get_billing_state(),
        'zipCode'         => $order->get_billing_postcode(),
    ),
    'products'        => $items_data, // <=== HERE
    'redirectUrl'     => $cburl,
    'referenceNumber' => $order->get_id(),
);

它应该按预期工作.

相关:

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

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