/**
  * prepare children
  *
  * @return array
  */
 protected function prepareChildren()
 {
     $result = array();
     if ($this->currentProduct->hasAttributes()) {
         $combinationImages = $this->currentProduct->getCombinationImages($this->getPlugin()->getLanguageId());
         // this function exists until the newest version (actual 1.6.0.14).
         // cause of the typo the function getAttributeCombinaisons is a wrapper function for getAttributeCombinations.
         $attributes = $this->currentProduct->getAttributeCombinaisons($this->getPlugin()->getLanguageId());
         $combinations = array();
         $attribute_groups = array();
         foreach ($attributes as $a) {
             $combinations[$a['id_product_attribute']][$a['id_attribute_group']] = $a;
             $attribute_groups[$a['id_attribute_group']] = $a['group_name'];
         }
         foreach ($combinations as $combination) {
             $childProduct = (object) current($combination);
             /**
              * global info
              */
             $childItemItem = new Shopgate_Model_Catalog_Product();
             $childItemItem->setIsChild(true);
             $childItemItem->setUid(sprintf(ShopgateItemsItem::DEFAULT_ITEM_ATTRIBUTE_DIVIDER_PATTERN, $this->currentProduct->id, $childProduct->id_product_attribute));
             /**
              * id default child
              */
             if ($childProduct->default_on == 1) {
                 $childItemItem->setIsDefaultChild(true);
             }
             /**
              * price
              */
             $priceItem = new Shopgate_Model_Catalog_Price();
             if ($childProduct->wholesale_price > 0 && $childProduct->wholesale_price != $this->currentProduct->wholesale_price) {
                 /**
                  * setCost
                  */
                 $priceItem->setCost($this->convertPrice($childProduct->wholesale_price));
             }
             if ($childProduct->minimal_quantity > 1) {
                 $priceItem->setMinimumOrderAmount($childProduct->minimal_quantity);
             }
             /**
              * base price
              */
             if ($this->currentProduct->unit_price != 0) {
                 if (isset($childProduct->unit_price_impact) && $childProduct->unit_price_impact != 0) {
                     $productPrice = $this->getItemPrice($this->currentProduct->id, $childProduct->id_product_attribute, true);
                     $basePrice = $productPrice / $this->currentProduct->unit_price_ratio + $childProduct->unit_price_impact;
                     if (!$this->getUseTax()) {
                         $basePrice /= ($this->currentProduct->tax_rate + 100) / 100;
                     }
                     $basePrice = number_format($basePrice, 2);
                     if ($this->currentProduct->unity != '') {
                         $priceItem->setBasePrice(sprintf('%s %s / %s', $basePrice, $this->getPlugin()->getContext()->currency->iso_code, $this->currentProduct->unity));
                     } else {
                         $priceItem->setBasePrice(sprintf('%s %s', $basePrice, $this->getPlugin()->getContext()->currency->iso_code));
                     }
                 }
             }
             $priceItem->setPrice($this->getItemPrice($this->currentProduct->id, $childProduct->id_product_attribute, $this->getUseTax()));
             $priceItem->setSalePrice($this->getItemPrice($this->currentProduct->id, $childProduct->id_product_attribute, $this->getUseTax(), true));
             $childItemItem->setPrice($priceItem);
             /**
              * tier prices
              */
             foreach ($this->getTierPrices($priceItem, $this->currentProduct->id, $childProduct->id_product_attribute) as $tierPriceRule) {
                 $priceItem->addTierPriceGroup($tierPriceRule);
             }
             /**
              * tax class
              */
             $childItemItem->setTaxClass($this->prepareTaxClass());
             /**
              * stock
              */
             $stockItem = new Shopgate_Model_Catalog_Stock();
             $stockItem->setStockQuantity($childProduct->quantity);
             $stockItem->setMinimumOrderQuantity($childProduct->minimal_quantity > 1 ? $childProduct->minimal_quantity : 0);
             /*
              * needs to be set, because an internal function of Prestashop (Product::checkQty) needs to know what attribute is set
              */
             $this->currentProduct->id_product_attribute = $childProduct->id_product_attribute;
             /*
              * we need to check this for each child because of the different stock quantity a attribute can have
              */
             $stockItem->setIsSaleable($this->prepareIsSaleable($this->currentProduct, $stockItem->getStockQuantity()));
             $stockItem->setAvailabilityText($this->prepareAvailableText($stockItem->getIsSaleable(), $stockItem->getStockQuantity(), $this->currentProduct->out_of_stock));
             $childItemItem->setStock($stockItem);
             /**
              * identifiers
              */
             foreach ($this->prepareIdentifiers($childProduct) as $identifier) {
                 $childItemItem->addIdentifier($identifier);
             }
             /**
              * attribute
              */
             foreach ($combination as $combinationItem) {
                 $attributeItem = new Shopgate_Model_Catalog_Attribute();
                 $attributeItem->setGroupUid($combinationItem['id_attribute_group']);
                 $attributeItem->setUid($combinationItem['id_attribute']);
                 $attributeItem->setLabel($combinationItem['attribute_name']);
                 $childItemItem->addAttribute($attributeItem);
             }
             /**
              * images
              */
             if (is_array($combinationImages) && array_key_exists($childProduct->id_product_attribute, $combinationImages)) {
                 foreach ($this->prepareImages($combinationImages[$childProduct->id_product_attribute]) as $image) {
                     $childItemItem->addImage($image);
                 }
             }
             $result[] = $childItemItem;
         }
     }
     return $result;
 }
Example #2
0
 /**
  * set children
  */
 public function setChildren()
 {
     parent::setChildren($this->getAdditionalInfo('children'));
 }
Example #3
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;
 }