Example #1
0
 /**
  * Initialize product(s) for add to cart process
  *
  * @param   Varien_Object $buyRequest
  * @return  array || string || false
  */
 public function prepareForCart(Varien_Object $buyRequest)
 {
     $productsInfo = $buyRequest->getSuperGroup();
     if (!empty($productsInfo) && is_array($productsInfo)) {
         $products = array();
         $associatedProducts = $this->getAssociatedProducts();
         if ($associatedProducts) {
             $productId = $this->getProduct()->getId();
             foreach ($associatedProducts as $subProduct) {
                 if (isset($productsInfo[$subProduct->getId()])) {
                     $qty = $productsInfo[$subProduct->getId()];
                     if (!empty($qty)) {
                         $_result = $subProduct->getTypeInstance()->prepareForCart($buyRequest);
                         if (is_string($_result) && !is_array($_result)) {
                             return $_result;
                         }
                         if (!isset($_result[0])) {
                             return Mage::helper('checkout')->__('Can not add item to shopping cart');
                         }
                         $_result[0]->setCartQty($qty);
                         $_result[0]->addCustomOption('product_type', self::TYPE_CODE, $this->getProduct());
                         $_result[0]->addCustomOption('info_buyRequest', serialize(array('super_product_config' => array('product_type' => self::TYPE_CODE, 'product_id' => $this->getProduct()->getId()))));
                         $products[] = $_result[0];
                     }
                 }
             }
         }
         if (count($products)) {
             return $products;
         }
     }
     return Mage::helper('catalog')->__('Please specify the product(s) quantity');
 }
 public function addProductAdvanced(Mage_Catalog_Model_Product $product, $request = null, $processMode = null)
 {
     if ($request === null) {
         $request = 1;
     }
     if (is_numeric($request)) {
         $request = new Varien_Object(array('qty' => $request));
     }
     if (!$request instanceof Varien_Object) {
         Mage::throwException(Mage::helper('sales')->__('Invalid request for adding product to quote.'));
     }
     switch ($product->getTypeId()) {
         case 'simple':
         case 'virtual':
         case 'downloadable':
         case 'configurable':
         case 'bundle':
             $qty = $request->getQty();
             if ($qty > 1) {
                 $request->setQty(1);
                 for ($i = 1; $i < $qty; $i++) {
                     $lineProduct = clone $product;
                     $lineRequest = clone $request;
                     $this->addProductAdvanced($lineProduct, $lineRequest, $processMode);
                 }
             }
             break;
         case 'grouped':
             $superGroup = $request->getSuperGroup();
             foreach ($superGroup as $productId => $qty) {
                 if ($qty > 1) {
                     for ($i = 1; $i < $qty; $i++) {
                         $customSuperGroup = $request->getSuperGroup();
                         foreach ($customSuperGroup as $customProductId => $customQty) {
                             $customSuperGroup[$customProductId] = $customProductId === $productId ? 1 : 0;
                         }
                         $lineProduct = clone $product;
                         $lineRequest = clone $request;
                         $lineRequest->setSuperGroup($customSuperGroup);
                         $this->addProductAdvanced($lineProduct, $lineRequest, $processMode);
                     }
                 }
                 $superGroup[$productId] = $qty > 0 ? 1 : 0;
             }
             $request->setSuperGroup($superGroup);
             break;
     }
     $cartCandidates = $product->getTypeInstance(true)->prepareForCartAdvanced($request, $product, $processMode);
     /**
      * Error message
      */
     if (is_string($cartCandidates)) {
         return $cartCandidates;
     }
     /**
      * If prepare process return one object
      */
     if (!is_array($cartCandidates)) {
         $cartCandidates = array($cartCandidates);
     }
     $parentItem = null;
     $errors = array();
     $items = array();
     foreach ($cartCandidates as $candidate) {
         // Child items can be sticked together only within their parent
         $stickWithinParent = $candidate->getParentProductId() ? $parentItem : null;
         $candidate->setStickWithinParent($stickWithinParent);
         $item = $this->_addCatalogProduct($candidate, $candidate->getCartQty());
         if ($request->getResetCount() && !$stickWithinParent && $item->getId() === $request->getId()) {
             $item->setData('qty', 0);
         }
         $items[] = $item;
         /**
          * As parent item we should always use the item of first added product
          */
         if (!$parentItem) {
             $parentItem = $item;
         }
         if ($parentItem && $candidate->getParentProductId()) {
             $item->setParentItem($parentItem);
         }
         /**
          * We specify qty after we know about parent (for stock)
          */
         $item->addQty($candidate->getCartQty());
         // collect errors instead of throwing first one
         if ($item->getHasError()) {
             $message = $item->getMessage();
             if (!in_array($message, $errors)) {
                 // filter duplicate messages
                 $errors[] = $message;
             }
         }
     }
     if (!empty($errors)) {
         Mage::throwException(implode("\n", $errors));
     }
     Mage::dispatchEvent('sales_quote_product_add_after', array('items' => $items));
     return $item;
 }
Example #3
0
 /**
  * Prepare selected qty for grouped product's options
  *
  * @param  Mage_Catalog_Model_Product $product
  * @param  Varien_Object $buyRequest
  * @return array
  */
 public function processBuyRequest($product, $buyRequest)
 {
     $superGroup = $buyRequest->getSuperGroup();
     $superGroup = is_array($superGroup) ? array_filter($superGroup, 'intval') : array();
     $options = array('super_group' => $superGroup);
     return $options;
 }
Example #4
0
 /**
  * Implement for compatibility with Magento pre 1.5.0 CE.
  *
  * @param Varien_Object $buyRequest
  * @param null $product
  * @return array|string
  */
 public function prepareForCart(Varien_Object $buyRequest, $product = null)
 {
     $product = $this->getProduct($product);
     $productsInfo = $buyRequest->getSuperGroup();
     if (!empty($productsInfo) && is_array($productsInfo)) {
         $products = array();
         $associatedProducts = $this->getAssociatedProducts($product);
         if ($associatedProducts) {
             foreach ($associatedProducts as $subProduct) {
                 if (isset($productsInfo[$subProduct->getId()])) {
                     $qty = $productsInfo[$subProduct->getId()];
                     if (!empty($qty) && is_numeric($qty)) {
                         /* BEGIN Brim Grouped-Options Customizations */
                         if ($subProduct->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE) {
                             $isConfigurable = true;
                             $configBuyRequest = clone $buyRequest;
                             $superAttr = $buyRequest->getSuperAttribute();
                             $configBuyRequest->setSuperAttribute($superAttr[$subProduct->getId()]);
                             $configBuyRequest->setQty($qty);
                             $_result = $subProduct->getTypeInstance(true)->prepareForCart($configBuyRequest, $subProduct);
                         } else {
                             /* END Brim Grouped-Options Customizations */
                             $isConfigurable = false;
                             $clonedBuyRequest = clone $buyRequest;
                             $superOptions = $buyRequest->getSuperOptions();
                             if ($superOptions && isset($superOptions[$subProduct->getId()])) {
                                 $clonedBuyRequest->setOptions($superOptions[$subProduct->getId()]);
                             }
                             if ($subProduct->getHasOptions() && count($subProduct->getOptions()) == 0) {
                                 foreach ($subProduct->getProductOptionsCollection() as $_option) {
                                     $subProduct->addOption($_option);
                                 }
                             }
                             $_result = $subProduct->getTypeInstance(true)->prepareForCart($clonedBuyRequest, $subProduct);
                         }
                         if (is_string($_result) && !is_array($_result)) {
                             return $_result;
                         }
                         if (!isset($_result[0])) {
                             return Mage::helper('checkout')->__('Cannot add the item to shopping cart.');
                         }
                         /* BEGIN Brim Grouped-Options Customizations */
                         if ($isConfigurable) {
                             foreach ($_result as $item) {
                                 $products[] = $item;
                             }
                         } else {
                             /* END Brim Grouped-Options Customizations */
                             $_result[0]->setCartQty($qty);
                             $_result[0]->addCustomOption('product_type', self::TYPE_CODE, $product);
                             $newBuyRequest = array('super_product_config' => array('product_type' => self::TYPE_CODE, 'product_id' => $product->getId()));
                             if (isset($clonedBuyRequest)) {
                                 if ($clonedBuyRequest->getOptions()) {
                                     $newBuyRequest['options'] = $clonedBuyRequest->getOptions();
                                 }
                                 if ($clonedBuyRequest->getQty()) {
                                     $newBuyRequest['qty'] = $clonedBuyRequest->getQty();
                                 }
                             }
                             $_result[0]->addCustomOption('info_buyRequest', serialize($newBuyRequest));
                             $products[] = $_result[0];
                         }
                     }
                 }
             }
         }
         if (count($products)) {
             return $products;
         }
     }
     return Mage::helper('catalog')->__('Please specify the quantity of product(s).');
 }