Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The logic found in Mage_Sales_Model_Service_Order::prepareInvoice, the method which you are ultimately calling to prepare the invoice, reveals what is at play here. The following loop is employed, and the inner else-if block is where the qty is set when an array of qtys is passed in:</p> <pre><code>foreach ($this-&gt;_order-&gt;getAllItems() as $orderItem) { if (!$this-&gt;_canInvoiceItem($orderItem, array())) { continue; } $item = $this-&gt;_convertor-&gt;itemToInvoiceItem($orderItem); if ($orderItem-&gt;isDummy()) { $qty = $orderItem-&gt;getQtyOrdered() ? $orderItem-&gt;getQtyOrdered() : 1; } else if (!empty($qtys)) { if (isset($qtys[$orderItem-&gt;getId()])) { $qty = (float) $qtys[$orderItem-&gt;getId()]; } } else { $qty = $orderItem-&gt;getQtyToInvoice(); } $totalQty += $qty; $item-&gt;setQty($qty); $invoice-&gt;addItem($item); } </code></pre> <p>The <code>$qtys</code> variable is the array which you are passing in to the prepareInvoice call. In your case, you are only passing an index for the item you wish to add to the invoice. Per the documentation (and the above loop), this <em>should</em> work, except for one minor problem: the above loop is not resetting the value of $qty at the top of the loop to 0. This does not pose a problem when called from the core code which processes initialization of the invoice when created from the admin via the pre-existing form, as the form which is being submitted will always contain a value for each item on the order, and in the case of only 1 item being invoiced, all others will hold a 0 qty value thus working around the failure to reset the value of $qty.</p> <p>To solve your problem, try setting your <code>$qtys</code> variable like so (I'm assuming 370 and 371 are the two order item entity ids):</p> <pre><code>$qtys = array( 370 =&gt; 1, 371 =&gt; 0, ); </code></pre> <p>An alternative which I might suggest would be to simply have your "Create Invoice" link set the appropriate values in the form to invoice the individual item and then submit the form directly. This way you'll be relying on the known-to-be-working core controller for the heavy lifting. This will, of course, only work if you are not doing anything fairly customized beyond invoicing the single item. :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload