Magento – Add products to order programmatically and have payment module see them

checkoutedit-orderevent-observermagento-1.9payment

I am building an extension that adds a number of items into an order once the user presses "place order" (after entering all their details.)

I am using the event observer sales_order_place_before

I'm adding the items to Mage::getModel('sales/quote_item') and Mage::getModel('sales/order_item')

The items appear in the order in the frontend and backend correctly.

However, the payment module I'm using (eWay Rapid) does not see the new items.
I confirmed this by turning on the extension debugging where it outputs the items on the order it's processing, my added items ARE NOT there.
I also confirmed that my extension DOES add the items BEFORE the payment module takes over.

I don't think it would be this payment module only, I think it's related to how Magento core works with any payment module when it passes the order details.

How can I add the items to the order and have the payment module aware?

Best Answer

Your problem is likely related to the items being cached in the getAllItems method

public function getAllItems()
{
    // We calculate item list once and cache it in three arrays - all items, nominal, non-nominal
    $cachedItems = $this->_nominalOnly ? 'nominal' : ($this->_nominalOnly === false ? 'nonnominal' : 'all');
    $key = 'cached_items_' . $cachedItems;
    if (!$this->hasData($key)) {

see https://github.com/OpenMage/magento-mirror/blob/magento-1.9/app/code/core/Mage/Sales/Model/Quote/Address.php#L447

The addItem methods in Magento do not invalidate this cache. So something like $address->unsetData('cached_items_all'); might work after you have added your product.

Related Topic