Quantcast
Channel: Josh Pratt – Prattski | Magento & Web Development
Viewing all articles
Browse latest Browse all 14

Magento: Adding Items to Existing Orders

$
0
0

Modifying existing orders is always going to be a risky task, especially since orders have multiple states and statuses. Be careful when doing anything like this.

I recently had a need to add an item to a bunch of orders that had not yet been shipped. Fortunately, it was a bit easier for me, because the situation I had to work from is that all orders with a specific SKU were missing another item (it should have been included in a bundle, but they forgot it when creating the bundle). So, it was easy to look up all orders that had that specific sku.

However you get your orders is up to you. I will walk you through what to do when you have your orders and are ready to loop through them and add the item.

I had to add the same exact item to all the orders I needed to modify. So first I loaded up a product object, and I had to set the price to zero because it would have been included for free in the bundle anyway:

// Load the Order
$product = Mage::getModel('catalog/product')->load($productId);
$product->setPrice(0);

In my case, I had an array of order ids that I looped through, so I had to load up each order:

// Load the Order
$order = Mage::getModel('sales/order')->load($orderId);

Then I had to get the quote. For some reason (not sure if a bug or something), $order->getQuote() did not work. Try that first. If that doesn’t work, try this:

// Load the Quote
$quote = Mage::getModel('sales/quote')->getCollection()->addFieldToFilter("entity_id", $order->getQuoteId())->getFirstItem();

In our case, we were finding that some orders did not have a corresponding quote (maybe a bug, maybe some of them got purged for some reason), so I had to add in some code to convert the current order to a quote:

// Create quote if no quote was found
if (!$quote->getId()) {
    $quote = Mage::getModel('sales/convert_order')
        ->toQuote($order)
        ->setIsActive(false)
        ->setReservedOrderId($order->getIncrementId())
        ->save();
}

Once we have the quote object, we can then add an item to it. Make sure you’ve loaded up your product object, because you will pass it in here. The method setProduct accepts a product object. You can also specify the qty if you need to add more than one:

// Create the item for the quote
$quoteItem = Mage::getModel('sales/quote_item')
    ->setProduct($product)
    ->setQuote($quote)
    ->setQty($qty)
    ->save();

Now that the item has been added to the quote, we need to convert the quote back to the order:

// Convert the quote item to an order item for this order
$orderItem = Mage::getModel('sales/convert_quote')
    ->itemToOrderItem($quoteItem)
    ->setOrderID($order->getId())
    ->save($orderId);

At that point, the order will then have the new item.

I couldn’t find a better way to add items to existing orders, so if you happen to know how to improve this, or a better way to do it, please let me know.


Viewing all articles
Browse latest Browse all 14