Example #1
0
 public function loadItems()
 {
     if (!$this->isExistingRecord()) {
         return false;
     }
     $this->event('before-load');
     $itemSet = $this->getRelatedRecordSet('OrderedItem', new ARSelectFilter(), array('Product', 'Category', 'DefaultImage' => 'ProductImage'));
     $this->orderedItems = $itemSet->getData();
     $products = $itemSet->extractReferencedItemSet('product');
     ProductPrice::loadPricesForRecordSet($products);
     $parentIDs = $products->extractReferencedItemSet('parent')->getRecordIDs();
     if ($parentIDs) {
         ActiveRecordModel::getRecordSet('Product', new ARSelectFilter(new INCond(new ARFieldHandle('Product', 'ID'), $parentIDs)), array('Category', 'ProductImage'));
     }
     if ($this->orderedItems) {
         if (!$this->shipments || !$this->shipments->size()) {
             $this->shipments = $this->getRelatedRecordSet('Shipment', new ARSelectFilter(), array('UserAddress', 'ShippingService'));
             // @todo: should be loaded automatically
             foreach ($this->shipments as $shipment) {
                 if ($shipment->shippingAddress->get()) {
                     $shipment->shippingAddress->get()->load();
                 }
             }
         }
         if (!$this->shipments->size() && !$this->isFinalized->get()) {
             $this->shipments = unserialize($this->shipping->get());
         }
         OrderedItemOption::loadOptionsForItemSet(ARSet::buildFromArray($this->orderedItems));
         ARSet::buildFromArray($this->orderedItems)->extractReferencedItemSet('product', 'ProductSet')->loadVariations();
         foreach ($this->orderedItems as $key => $item) {
             if ($item->parent->get()) {
                 $item->parent->get()->registerSubItem($item);
                 unset($this->orderedItems[$key]);
             }
         }
         Product::loadAdditionalCategoriesForSet(ARSet::buildFromArray($this->orderedItems)->extractReferencedItemSet('product'));
     }
     if (!$this->isFinalized->get() && $this->orderedItems) {
         return $this->updateToStock();
     }
     $this->event('after-load');
 }