简体   繁体   中英

Magento Sales Order Invoice with product quantity updation using API not working properly

I'm creating a Magento application and I'm planing to create sales order invoice by using Magento API.

Here is my pseudo code for my invoice creation. The problem is it creates an invoice but that invoice is always blank (not showing products and quantity)

<?php 

$proxy = new SoapClient('http://myurl/api/soap?wsdl');
$sessionId = $proxy->login('apiuser', 'apikey');

// item array with sku and quantity
$invoiceItems = array(
    '002' => '1', '003' => '1', '004' => '1', '005' => '1'
);

// Create new invoice
$newInvoiceId = $proxy->call($sessionId, 'sales_order_invoice.create', array($saleorderno, $invoiceItems, 'Invoice Created', true, true));

?>

But when I'm creating an sales order invoice like this (there's no changes in quantity from sales order), it works properly

$newInvoiceId = $proxy->call($sessionId, 'sales_order_invoice.create', array($saleorderno, array(), 'Invoice Created', true, true));

Is there any mistakes with my code? Can any one give some advice for me?

In the array variable " $invoiceItems ", you are providing this value:-

$invoiceItems = array(
    '002' => '1',
    '003' => '1',
    '004' => '1',
    '005' => '1'
);

The keys for the above array must correspond to the Order Item ID and not to the Item SKU. This means that whenever an order is placed, each ordered item get its own unique Order Item ID, which is not at all same as that of the corresponding SKU or the corresponding Product ID.

So to get this, you need to load the Order Collection from the Order ID, and fetch the Items Collection list as below:-

$saleorderno = 'SOME VALID ORDER INCREMENT ID';
$order = Mage::getModel('sales/order')->loadByIncrementId($saleorderno);

$orderItems = $order->getAllItems();
$invoiceItems = array();

foreach ($orderItems as $_eachItem) {
    $invoiceItems[$_eachItem->getItemId()] = $_eachItem->getQtyOrdered();
}

$newInvoiceId = $proxy->call($sessionId, 'sales_order_invoice.create', array($saleorderno, $invoiceItems, 'Invoice Created', true, true));

Now this above code should work for you.

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM