Close

November 23, 2015

Magento EE Check If Order Item Has Been RMA’d

One great feature of Magento Enterprise is that it comes with an RMA system completely built in. Its a very good feature to have if you have a high volume of sales. The system is a bit confusing to learn, from a user interface perspective, but once you get the hang of it its not that difficult.

From a programmatic perspective, it makes sending out order specific information a bit more complicated and will also affect any sales data forecasting tools that you have custom built. Below is a snippet of code to help determine whether an order has any RMA’d items, programmatically.

Below is a quick snippet of code to load an order, go through each of its items, and determine if the item has been RMA’d. Obviously this is a very basic “hello world” application demonstrating the concept in Magento EE, but it can be used for various other scripts.

 
$id=123456; //order id as an example
 
$order=Mage::getModel('sales/order')->load($id); //use an entity id here
$rma_items=getRmaItemsByOrder($id);//get our rma items..just in case
 
//load our list of items.
$items = $order->getItemsCollection();
foreach($items as $item):
    $itemid=$item->getId();
    print "<BR>ITEM: ".$item->getId();
    if (in_array($itemid, $rma_items)):
      //Do your RMA work here.
      print "Item was RMA'd"; 
 
    endif;
 
endforeach;
 
 
//Given an order id (NOT incrementid), return a list of product ids that have been RMA'd successfully
 function getRmaItemsByOrder($orderId)
    {   
 
        $connection = Mage::getSingleton('core/resource');
        $adapter = $connection->getConnection('core_read');
 
        $subSelect = $adapter->select()
            ->from(
                array('main' => Mage::getSingleton('core/resource')->getTableName('enterprise_rma/rma')),
                array()
            )
            ->where('main.order_id = ?', $orderId)
            ->where('main.status NOT IN (?)',
                array(
                    Enterprise_Rma_Model_Rma_Source_Status::STATE_REJECTED_ON_ITEM
 
                )
            );
 
        $select = $adapter->select()
            ->from(
                array('item_entity' => Mage::getSingleton('core/resource')->getTableName('enterprise_rma/item_entity')),
                array('item_entity.order_item_id','item_entity.order_item_id')
            )
            ->exists($subSelect, 'main.entity_id = item_entity.rma_entity_id');
 
        return array_values($adapter->fetchPairs($select));
    }