Example #1
0
 /**
  * Add a product to the collection
  * @param   object
  * @param   integer
  * @param   array
  * @return  ProductCollectionItem
  */
 public function addProduct(IsotopeProduct $objProduct, $intQuantity, array $arrConfig = array())
 {
     // !HOOK: additional functionality when adding product to collection
     if (isset($GLOBALS['ISO_HOOKS']['addProductToCollection']) && is_array($GLOBALS['ISO_HOOKS']['addProductToCollection'])) {
         foreach ($GLOBALS['ISO_HOOKS']['addProductToCollection'] as $callback) {
             $objCallback = \System::importStatic($callback[0]);
             $intQuantity = $objCallback->{$callback}[1]($objProduct, $intQuantity, $this);
         }
     }
     if ($intQuantity == 0) {
         return false;
     }
     $time = time();
     $this->tstamp = $time;
     // Make sure collection is in DB before adding product
     if (!\Model\Registry::getInstance()->isRegistered($this)) {
         $this->save();
     }
     // Remove uploaded files from session so they are not added to the next product (see #646)
     unset($_SESSION['FILES']);
     $objItem = $this->getItemForProduct($objProduct);
     $intMinimumQuantity = $objProduct->getMinimumQuantity();
     if (null !== $objItem) {
         if ($objItem->quantity + $intQuantity < $intMinimumQuantity) {
             $_SESSION['ISO_INFO'][] = sprintf($GLOBALS['TL_LANG']['ERR']['productMinimumQuantity'], $objProduct->name, $intMinimumQuantity);
             $intQuantity = $intMinimumQuantity - $objItem->quantity;
         }
         $objItem->increaseQuantityBy($intQuantity);
         return $objItem;
     } else {
         if ($intQuantity < $intMinimumQuantity) {
             $_SESSION['ISO_INFO'][] = sprintf($GLOBALS['TL_LANG']['ERR']['productMinimumQuantity'], $objProduct->name, $intMinimumQuantity);
             $intQuantity = $intMinimumQuantity;
         }
         $objItem = new ProductCollectionItem();
         $objItem->pid = $this->id;
         $objItem->tstamp = $time;
         $objItem->type = array_search(get_class($objProduct), Product::getModelTypes());
         $objItem->product_id = $objProduct->{$objProduct->getPk()};
         $objItem->sku = (string) $objProduct->sku;
         $objItem->name = (string) $objProduct->name;
         $objItem->options = $objProduct->getOptions();
         $objItem->quantity = (int) $intQuantity;
         $objItem->price = (double) ($objProduct->getPrice($this) ? $objProduct->getPrice($this)->getAmount((int) $intQuantity) : 0);
         $objItem->tax_free_price = (double) ($objProduct->getPrice($this) ? $objProduct->getPrice($this)->getNetAmount((int) $intQuantity) : 0);
         $objItem->jumpTo = (int) $arrConfig['jumpTo']->id;
         $objItem->save();
         // Add the new item to our cache
         $this->arrItems[$objItem->id] = $objItem;
         return $objItem;
     }
 }