/** * 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; }
/** * Get all items shipping to a given address. For billing addresses, this * will be all virtual items in the order. For shipping addresses, any * non-virtual items. Only items that are to be included in the order create * request should be returned. * * @param Mage_Customer_Model_Address_Abstract * @param Mage_Sales_Model_Resource_Order_Item_Collection * @return Mage_Sales_Model_Order_Item[] */ protected function _getItemsForAddress(Mage_Customer_Model_Address_Abstract $address, Mage_Sales_Model_Resource_Order_Item_Collection $orderItems) { // All items will have an `order_address_id` matching the id of the // address the item ships to (including virtual items which "ship" to // the billing address). // Filter the given collection instead of using address methods to get // items to prevent loading separate item collections for each address. return $this->_itemSelection->selectFrom($orderItems->getItemsByColumnValue('order_address_id', $address->getId())); }
/** * 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; }