1

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

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?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

1 Answers1

0

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

Then this will be used in:

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

It should work as expected.

Related:

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399