示例#1
0
 public function getProductOptionCollection(Mage_Catalog_Model_Product $product)
 {
     $action = Mage::app()->getFrontController()->getAction()->getFullActionName();
     if ($action == "catalog_category_view") {
         return parent::getProductOptionCollection($product);
     }
     $helper = Mage::helper('mageworx_customoptions');
     $collection = $this->getCollection()->addFieldToFilter('product_id', $product->getId())->addTitleToResult($product->getStoreId())->addPriceToResult($product->getStoreId())->addViewModeToResult($product->getStoreId())->addDescriptionToResult($product->getStoreId())->addDefaultTextToResult($product->getStoreId())->setOrder('sort_order', 'asc')->setOrder('title', 'asc');
     $isProductEditPage = Mage::app()->getStore()->isAdmin() && Mage::app()->getRequest()->getControllerName() == 'catalog_product';
     if ($isProductEditPage) {
         $collection->addTemplateTitleToResult();
     }
     $collection->addValuesToResult($product->getStoreId());
     if (!$isProductEditPage) {
         // filter by view_mode
         $isRequire = false;
         foreach ($collection as $key => $item) {
             // 0-Disable, 1-Visible, 2-Hidden, 3-Backend, 4-Admin Only
             if ($item->getViewMode() == 0 && !is_null($item->getViewMode())) {
                 $collection->removeItemByKey($key);
             } elseif (!Mage::app()->getStore()->isAdmin() && ($item->getViewMode() == 3 || $item->getViewMode() == 4)) {
                 $collection->removeItemByKey($key);
             } elseif ($item->getIsRequire(true)) {
                 $isRequire = true;
             }
         }
         if (!$isRequire) {
             $product->setRequiredOptions(0);
         }
         if (count($collection) == 0) {
             $product->setHasOptions(0);
         }
         $customerGroupId = $helper->getCustomerGroupId();
         // filter by CustomerGroups
         if ($helper->isCustomerGroupsEnabled()) {
             $isRequire = false;
             foreach ($collection as $key => $item) {
                 $groups = $item->getCustomerGroups();
                 if ($groups !== '' && !in_array($customerGroupId, explode(',', $groups))) {
                     $collection->removeItemByKey($key);
                 } elseif ($item->getIsRequire(true)) {
                     $isRequire = true;
                 }
             }
             if (!$isRequire) {
                 $product->setRequiredOptions(0);
             }
             if (count($collection) == 0) {
                 $product->setHasOptions(0);
             }
         }
         $storeViewId = Mage::app()->getStore()->getId();
         // filter by StoreViews
         if ($helper->isStoreViewsEnabled()) {
             $isRequire = false;
             foreach ($collection as $key => $item) {
                 $storeViews = $item->getStoreViews();
                 if ($storeViews !== '' && !in_array($storeViewId, explode(',', $storeViews))) {
                     $collection->removeItemByKey($key);
                 } elseif ($item->getIsRequire(true)) {
                     $isRequire = true;
                 }
             }
             if (!$isRequire) {
                 $product->setRequiredOptions(0);
             }
             if (count($collection) == 0) {
                 $product->setHasOptions(0);
             }
         }
         // recheck inventory
         if ($product->getRequiredOptions()) {
             if ($helper->isInventoryEnabled() && ($helper->getOutOfStockOptions() == 1 || $helper->isSetProductOutOfStock())) {
                 $isDependentEnabled = $helper->isDependentEnabled();
                 // checkDependentInventory for parent -> set parent option "Out of stock"
                 if ($isDependentEnabled) {
                     foreach ($collection as $option) {
                         if ($this->getGroupByType($option->getType()) != self::OPTION_GROUP_SELECT || count($option->getValues()) == 0) {
                             continue;
                         }
                         foreach ($option->getValues() as $value) {
                             if (!$value->getDependentIds()) {
                                 continue;
                             }
                             $customoptionsQty = $helper->getCustomoptionsQty($value->getCustomoptionsQty(), $value->getSku(), $product->getId(), $value->getExtra(), $value);
                             if ($customoptionsQty !== 0 && !$this->checkDependentInventory($collection, $value, $product)) {
                                 $value->setCustomoptionsQty(0);
                             }
                         }
                     }
                 }
                 // if all required options "Out of stock" -> set product "Out of stock"
                 foreach ($collection as $option) {
                     if (!$option->getIsRequire(true) || $isDependentEnabled && $option->getIsDependent() || $this->getGroupByType($option->getType()) != self::OPTION_GROUP_SELECT || count($option->getValues()) == 0) {
                         continue;
                     }
                     $outOfStockFlag = true;
                     foreach ($option->getValues() as $value) {
                         $customoptionsQty = $helper->getCustomoptionsQty($value->getCustomoptionsQty(), $value->getSku(), $product->getId(), $value->getExtra(), $value);
                         if (is_numeric($customoptionsQty) && $customoptionsQty != 0) {
                             if ($isDependentEnabled && !$this->checkDependentInventory($collection, $value, $product)) {
                                 continue;
                             }
                             $outOfStockFlag = false;
                             break;
                         }
                     }
                     if ($outOfStockFlag) {
                         $product->setData('is_salable', false);
                         break;
                     }
                 }
             }
         }
     }
     // add images, special_prices, tier_prices
     $specialPriceEnabled = $helper->isSpecialPriceEnabled();
     $tierPriceEnabled = $helper->isTierPriceEnabled();
     $helper->getCustomerGroups();
     // init customer_groups for sort prices
     foreach ($collection as $key => $option) {
         if ($this->getGroupByType($option->getType()) == self::OPTION_GROUP_SELECT) {
             $values = $option->getValues();
             if (count($values) == 0) {
                 continue;
             }
             foreach ($values as $value) {
                 // add images to optionValue
                 $value->setImages($this->getOptionValueImages($value->getOptionTypeId()));
                 // link data (price, special_price, group_prices, tier_prices) by sku -> and skip special_prices, tier_prices from option
                 if (!$helper->applyLinkedBySkuDataToOption($value, $value->getSku(), $product->getStore(), $product->getTaxClassId())) {
                     // add special_prices
                     if ($specialPriceEnabled) {
                         $value->setSpecials($this->getOptionValueSpecialPrices($value->getOptionTypePriceId()));
                     }
                     // add tier_prices
                     if ($tierPriceEnabled) {
                         $value->setTiers($this->getOptionValueTierPrices($value->getOptionTypePriceId()));
                     }
                 }
                 $helper->calculateOptionSpecialPrice($value, $product, $specialPriceEnabled);
                 $value->setIsPriceCalculated(true);
             }
         } else {
             if ($helper->applyLinkedBySkuDataToOption($option, $option->getSku(), $product->getStore(), $product->getTaxClassId())) {
                 $helper->calculateOptionSpecialPrice($option, $product, false);
             }
             $option->setIsPriceCalculated(true);
         }
     }
     return $collection;
 }