首页 > 解决方案 > PHP array add an extra same-array

问题描述

I create an API for a external invoice system. Everything works, but i can not add the shipping costs as an new line_item in the invoice.

Following some questions, the code has now been added more extensively.

By adding an exact same array ($ line_items), I hoped it worked. However, the system will then stop running.

function create_invoice($email,$order,$customer_id){


    $api = new JorttBV_Client(APPID, APPTOKEN);
    $items = $order->get_items();

    //Shipping data
    $shippingcount = $order_data['shipping_total'];
    $shippingname = 'Verzending';

    //Start arrays
    $line_items = array();

   //Add shipping costs
    $line_items[] = array(
        'vat' => 21, 
        'amount' => $shippingcount,
        'quantity' => 1,
        'description' => $shippingname
        );

    //Loop product data in array
    foreach ( $items as $item_id => $item_data ) {
        $tax =  $item_data->get_total();
        $tax2 = $item_data->get_total_tax();
        $tax3 = ($tax2/$tax)*100;
        $line_items[] = array(
            'vat' => 21, 
            'amount' => number_format((float)$order->get_item_meta($item_id, '_line_subtotal', true),2,'.',''),
            'quantity' => $order->get_item_meta($item_id, '_qty', true),
            'description' => $item_data['name']
        );
    }

  //Add data to invoice
    $invoice = [
        'invoice' => [
            'customer_id' => $customer_id,
            'delivery_period' => '',
            "reference" => '',
            "remarks" => '',
            'line_items' => $line_items
        ]
    ];

    $result = $api->request($invoice, 'invoices');

    $res = json_decode($result);
    /* echo "<pre>"; print_r($invoice);
    echo "<pre>"; print_r($res);
    die("1233"); */
    $invoice_id =$res->invoice_id;

    send_invoice($email,$invoice_id);

}

Any idea?

标签: php

解决方案


推荐阅读