Получить массив конкретных данных из элементов заказа в WooCommerce 3 - PullRequest
1 голос
/ 25 сентября 2019

Мне нужен следующий ответ в 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;

Почему яполучать только первый элемент в массиве, когда выводится переменная?

1 Ответ

0 голосов
/ 25 сентября 2019

В вашем коде есть небольшие ошибки ... Попробуйте следующее, где вы получите конкретные данные для всех элементов заказа в вашей переменной, которые будут использоваться в вашем аргументе '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(),
);

Должно работать как положено.

Связано:

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...