• Resolved ???? ????

    (@vandanojan)


    Hi,
    I’m having an issue with retrieving “order line items” properties via the WooCommerce REST API. To sync orders from a source site to a destination one, I used the following code snippet, but it failed to display any properties at all on orders:

    
    if (isset($order['line_items']) && is_array($order['line_items'])) {
        
        $line_items_data = array();
        
        foreach ($order['line_items'] as $item) {
           
            $line_items_data[] = array(
                'id' => $item['id'], 
                'name' => $item['name'],
                'quantity' => $item['quantity'],
                'price' => $item['price'],
                'subtotal' => $item['subtotal'],
                'total' => $item['total'],
                'product_id' => $item['product_id'],
               
            );
        }
    }
    

    I initially removed the line ‘id’ => $item[‘id’] in $line_items_data[] array and the problem got temporarily solved, so now I had at least the above properties on the orders. but this led to the repetition of each order’s items and an increase in total price. This seems to be due to the lack of the identifier I had removed earlier. My question, therefore, is about the correct method of retrieving “id” in order line items.
    Thanks in advance


Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter ???? ????

    (@vandanojan)

    The above code was incomplete, here is the modified one:

    if (isset($order['line_items']) && is_array($order['line_items'])) {
        
        $line_items_data = array();
        
        foreach ($order['line_items'] as $item) {
           
            $line_items_data[] = array(
                'id' => $item['id'], 
                'name' => $item['name'],
                'quantity' => $item['quantity'],
                'price' => $item['price'],
                'subtotal' => $item['subtotal'],
                'total' => $item['total'],
                'product_id' => $item['product_id'],
               
            );
        }
    }
    
     $order_data = array(
                        'status' => $order['status'],
                        'customer_id' => $order['customer_id'],
                        'billing' => array(
                            'first_name' => $first_name,
                            'last_name' => $last_name,
                            'phone' => $phone,
                        ),
                        'line_items' => $line_items_data,
    //etc
    );
    anastas10s

    (@anastas10s)

    Hi there @vandanojan ??

    Helping out with custom coding of this nature is outside the scope of support that our support staff can help out with here, although I would recommend the following:

    • Joining our WooCommerce Slack community (it does have a developer channel where you can ask coding questions):?https://woo.com/community-slack/
    • Running the exact question you’re asking, along with the code provided, through an AI platform like ChatGPT for recommendations/changes to your code;

    I hope this is helpful! Please let us know if you have any further questions about this matter, or if we misinterpret your concern in any way. We will be happy to help you further.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Problem to retrieve order line_items properties via WC REST API’ is closed to new replies.