Exemplo n.º 1
0
 /**
  * @param $inputs
  *
  * @return array
  */
 protected function _setBundleOptions($inputs = array())
 {
     $bundleOptions = $this->item->getPriceModel()->getOptions($this->item);
     $isGross = Mage::getStoreConfig(self::CONFIG_XML_PATH_PRICE_INCLUDES_TAX, $this->_getConfig()->getStoreViewId());
     $stock = parent::getStock();
     $selectionQuantities = array();
     foreach ($bundleOptions as $bundleOption) {
         $optionValues = array();
         /* @var $bundleOption Mage_Bundle_Model_Option */
         if (!is_array($bundleOption->getSelections())) {
             $stock->setIsSaleable(false);
             $bundleOption->setSelections(array());
         }
         $optionPrices = array();
         $cheapestOptionId = null;
         $cheapestPrice = null;
         // fetch id of cheapest option
         foreach ($bundleOption->getSelections() as $selection) {
             $selectionPrice = $this->item->getPriceModel()->getSelectionFinalPrice($this->item, $selection, 1, $selection->getSelectionQty());
             $selectionPrice = Mage::helper('tax')->getPrice($selection, $selectionPrice, $isGross);
             if ($cheapestPrice === null || $cheapestPrice > $selectionPrice) {
                 $cheapestPrice = $selectionPrice;
                 $cheapestOptionId = $selection->getSelectionId();
             }
             $optionPrices[$selection->getSelectionId()] = $selectionPrice;
         }
         foreach ($bundleOption->getSelections() as $selection) {
             $option = new Shopgate_Model_Catalog_Option();
             /** @var $selection Mage_Catalog_Model_Product */
             $selectionId = $selection->getSelectionId();
             $qty = max(1, (int) $selection->getSelectionQty());
             $selectionPrice = $optionPrices[$selectionId];
             $selectionName = $qty > 1 ? $qty . " x " : '';
             $selectionName .= $this->_getMageCoreHelper()->htmlEscape($selection->getName());
             if (!array_key_exists($selectionId, $selectionQuantities)) {
                 $selectionQuantities[$selectionId] = 0;
             }
             if ($this->item->getStockItem()->getManageStock() && $selection->isSaleable() && $this->item->getStockItem()->getQty() > 0) {
                 if ($selectionQuantities[$selectionId] !== null) {
                     $selectionQuantities[$selectionId] += $this->item->getStockItem()->getQty();
                 }
             } else {
                 if (!$this->item->getStockItem()->getManageStock()) {
                     $selectionQuantities[$selectionId] = null;
                 } else {
                     if (!$selection->isSaleable() && $this->item->getStockItem()->getBackorders()) {
                         $selectionQuantities[$selectionId] = null;
                     } else {
                         $selectionQuantities[$selectionId] = 0;
                     }
                 }
             }
             $option->setUid($selection->getSelectionId());
             $option->setLabel($selectionName);
             $option->setSortOrder($selection->getPosition());
             // reset selection price, in this case the bundle parent is already configured
             // with the price of the cheapest bundle configuration
             if ($this->item->getPriceType() == Mage_Bundle_Model_Product_Price::PRICE_TYPE_DYNAMIC) {
                 if ($cheapestOptionId == $selection->getSelectionId() && $selection->getOption()->getRequired()) {
                     $selectionPrice = 0;
                 } elseif ($selection->getOption()->getRequired()) {
                     $selectionPrice = $selectionPrice - $optionPrices[$cheapestOptionId];
                 }
             }
             $option->setAdditionalPrice($selectionPrice);
             $optionValues[] = $option;
             if ($selectionQuantities[$selectionId] === null) {
                 unset($selectionQuantities[$selectionId]);
             }
         }
         $inputItem = new Shopgate_Model_Catalog_Input();
         $inputItem->setUid($bundleOption->getId());
         $inputItem->setType($this->_mapInputType($bundleOption->getType()));
         $title = $bundleOption->getTitle() ? $bundleOption->getTitle() : $bundleOption->getDefaultTitle();
         $inputItem->setLabel($title);
         $inputItem->setValidation($this->_buildInputValidation($inputItem->getType(), null));
         $inputItem->setRequired($bundleOption->getRequired());
         $inputItem->setSortOrder($bundleOption->getPosition());
         $inputItem->setOptions($optionValues);
         $inputs[] = $inputItem;
     }
     $stockQty = count($selectionQuantities) ? min($selectionQuantities) : 0;
     $stock->setStockQuantity($stockQty);
     if (!count($selectionQuantities)) {
         $stock->setUseStock(false);
     }
     $this->setStock($stock);
     return $inputs;
 }