【问题标题】:Get an array of specific data from order items in WooCommerce 3从 WooCommerce 3 中的订单项中获取一组特定数据
【发布时间】:2020-01-25 17:46:38
【问题描述】:

我需要 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 的第一个实例:

    $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;

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

【问题讨论】:

    标签: php json wordpress woocommerce jagged-arrays


    【解决方案1】:

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

    $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(),
    );
    

    它应该按预期工作。

    相关:

    【讨论】:

      猜你喜欢
      • 2018-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-26
      • 2020-01-11
      • 2021-03-20
      • 2014-08-22
      • 2019-03-31
      相关资源
      最近更新 更多