/** * 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; }