/** * Removes parent order items from collection * * @param $collection Mage_Sales_Model_Resource_Order_Item_Collection * @return array */ public function filterOrderItems(Mage_Sales_Model_Resource_Order_Item_Collection $collection) { $result = array(); /* remove parent items */ foreach ($collection as $item) { $collection->removeItemByKey($item->getParentItemId()); } /* calculate total ordered quantity per item */ foreach ($collection as $item) { if (isset($result[$item->getSku()])) { $result[$item->getSku()] += $item->getQtyOrdered(); } else { $result[$item->getSku()] = $item->getQtyOrdered(); } } return $result; }
/** * This is an internal function, given orderId and a collection, return an array of order items * This function is used by both getOrderItems and getOrderItemsForAdmin * * @param int $orderId * @param Mage_Sales_Model_Resource_Order_Item_Collection $orderItemsCollection * @param int|bool $parentId if need retrieves only bundle and its children * @return type */ protected function _getOrderItemsFromCollection($orderId, $orderItemsCollection, $parentId) { $getItemsIdsByOrder = $this->getItemsIdsByOrder($orderId); if (!$orderItemsCollection->count()) { return $orderItemsCollection; } /** * contains data that defines possibility of return for an order item * array value ['self'] refers to item's own rules * array value ['child'] refers to rules defined from item's sub-items */ $parent = array(); /** @var $product Mage_Catalog_Model_Product */ $product = Mage::getModel('catalog/product'); foreach ($orderItemsCollection as $item) { /* retrieves only bundle and children by $parentId */ if ($parentId && $item->getId() != $parentId && $item->getParentItemId() != $parentId) { $orderItemsCollection->removeItemByKey($item->getId()); continue; } $allowed = true; /* checks item in active rma */ if (in_array($item->getId(), $getItemsIdsByOrder)) { $allowed = false; } /* checks enable on product level */ $product->reset(); $product->setStoreId($item->getStoreId()); try { $product->load($item->getProductId()); } catch (Enterprise_AdminGws_Controller_Exception $e) { Mage::logException($e); $allowed = false; } if (!Mage::helper('enterprise_rma')->canReturnProduct($product, $item->getStoreId())) { $allowed = false; } if ($item->getParentItemId()) { if (!isset($parent[$item->getParentItemId()]['child'])) { $parent[$item->getParentItemId()]['child'] = false; } if (!$allowed) { $item->setIsOrdered(1); $item->setAvailableQty($item->getQtyShipped() - $item->getQtyRefunded() - $item->getQtyCanceled()); } $parent[$item->getParentItemId()]['child'] = $parent[$item->getParentItemId()]['child'] || $allowed; $parent[$item->getItemId()]['self'] = false; } else { $parent[$item->getItemId()]['self'] = $allowed; } } $bundle = false; foreach ($orderItemsCollection as $item) { if (isset($parent[$item->getId()]['child']) && ($parent[$item->getId()]['child'] === false || $parent[$item->getId()]['self'] == false)) { $orderItemsCollection->removeItemByKey($item->getId()); $bundle = $item->getId(); continue; } if ($bundle && $item->getParentItemId() && $bundle == $item->getParentItemId()) { $orderItemsCollection->removeItemByKey($item->getId()); } elseif (isset($parent[$item->getId()]['self']) && $parent[$item->getId()]['self'] === false) { if ($item->getParentItemId() && $bundle != $item->getParentItemId()) { } else { $orderItemsCollection->removeItemByKey($item->getId()); continue; } } if ($item->getProductType() == Mage_Catalog_Model_Product_Type::TYPE_BUNDLE && !isset($parent[$item->getId()]['child'])) { $orderItemsCollection->removeItemByKey($item->getId()); continue; } if ($item->getProductType() == Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE) { $productOptions = $item->getProductOptions(); $product->reset(); $product->load($product->getIdBySku($productOptions['simple_sku'])); if (!Mage::helper('enterprise_rma')->canReturnProduct($product, $item->getStoreId())) { $orderItemsCollection->removeItemByKey($item->getId()); continue; } } $item->setName($this->getProductName($item)); } return $orderItemsCollection; }