Exemplo n.º 1
0
 /**
  * Add or Move item product to shopping cart
  *
  * Return true if product was successful added or exception with code
  * Return false for disabled or unvisible products
  *
  * @throws Mage_Core_Exception
  * @param Mage_Checkout_Model_Cart $cart
  * @param int $qty
  * @return bool
  */
 public function addToCart(Mage_Checkout_Model_Cart $cart, $qty)
 {
     $product = $this->_getProduct();
     $storeId = $this->getStoreId();
     if ($this->getQty() < $qty + $this->getQtyFulfilled()) {
         $qty = $this->getQty() - $this->getQtyFulfilled();
     }
     if ($product->getStatus() != Mage_Catalog_Model_Product_Status::STATUS_ENABLED) {
         return false;
     }
     if (!$product->isVisibleInSiteVisibility()) {
         if ($product->getStoreId() == $storeId) {
             return false;
         }
         $urlData = Mage::getResourceSingleton('catalog/url')->getRewriteByProductStore(array($product->getId() => $storeId));
         if (!isset($urlData[$product->getId()])) {
             return false;
         }
         $product->setUrlDataObject(new Varien_Object($urlData));
         $visibility = $product->getUrlDataObject()->getVisibility();
         if (!in_array($visibility, $product->getVisibleInSiteVisibilities())) {
             return false;
         }
     }
     if (!$product->isSalable()) {
         Mage::throwException(Mage::helper('enterprise_giftregistry')->__('This product(s) is currently out of stock.'));
     }
     $product->setGiftregistryItemId($this->getId());
     $product->addCustomOption('giftregistry_id', $this->getEntityId());
     $request = unserialize($this->getCustomOptions());
     $request['qty'] = $qty;
     $cart->addProduct($product, $request);
     if (!empty($request['related_product'])) {
         $cart->addProductsByIds(explode(',', $request['related_product']));
     }
     if (!$product->isVisibleInSiteVisibility()) {
         $cart->getQuote()->getItemByProduct($product)->setStoreId($storeId);
     }
 }
 /**
  * Add or Move item product to shopping cart
  *
  * Return true if product was successful added or exception with code
  * Return false for disabled or unvisible products
  *
  * @throws Mage_Core_Exception
  * @param Mage_Checkout_Model_Cart $cart
  * @param int $qty
  * @return bool
  */
 public function addToCart(Mage_Checkout_Model_Cart $cart, $qty)
 {
     $product = $this->_getProduct();
     $storeId = $this->getStoreId();
     if ($this->getQty() < $qty + $this->getQtyFulfilled()) {
         $qty = $this->getQty() - $this->getQtyFulfilled();
     }
     if ($product->getStatus() != Mage_Catalog_Model_Product_Status::STATUS_ENABLED) {
         return false;
     }
     if (!$product->isVisibleInSiteVisibility()) {
         if ($product->getStoreId() == $storeId) {
             return false;
         }
     }
     if (!$product->isSalable()) {
         Mage::throwException(Mage::helper('enterprise_giftregistry')->__('This product(s) is currently out of stock.'));
     }
     $product->setGiftregistryItemId($this->getId());
     $product->addCustomOption('giftregistry_id', $this->getEntityId());
     $request = $this->getBuyRequest();
     $request->setQty($qty);
     $cart->addProduct($product, $request);
     $relatedProduct = $request->getRelatedProduct();
     if (!empty($relatedProduct)) {
         $cart->addProductsByIds(explode(',', $relatedProduct));
     }
     if (!$product->isVisibleInSiteVisibility()) {
         $cart->getQuote()->getItemByProduct($product)->setStoreId($storeId);
     }
 }
Exemplo n.º 3
0
 /**
  * Get array of product ids.
  * If product not need option, then it will go to cart with success mmessage.
  * If product need to option's selection print message about it.
  * 
  * @param arrai|int  $productIds
  * @return AW_Boughttogether_Model_Cart
  */
 public function addProductsByIds($productIds)
 {
     if ($productIds && is_array($productIds)) {
         if ($this->_isSomebodyComposite($productIds)) {
             try {
                 foreach ($productIds as $productId) {
                     $product = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())->load($productId);
                     if (isset($product) && !($product->isComposite() || $this->_hasRequiredOptions($product)) && $product->isSaleable()) {
                         $this->addProduct($product);
                         $message = Mage::helper('checkout')->__('%s was successfully added to your shopping cart.', $product->getName());
                         $this->getCheckoutSession()->addSuccess($message);
                     } elseif (isset($product) && ($product->isComposite() || $this->_hasRequiredOptions($product)) && $product->isSaleable()) {
                         $link = '<a href="' . Mage::getUrl('catalog/product/view', array('id' => $productId)) . '">' . $product->getName() . '</a>';
                         $error = Mage::helper('boughttogether')->__('Product %s can\'t be added to cart. Please specify the product option(s) first.', $link);
                         $this->getCheckoutSession()->addError($error);
                     }
                 }
             } catch (Exception $e) {
                 $this->getCheckoutSession()->addException($e, $e->getMessage());
             }
         } else {
             parent::addProductsByIds($productIds);
         }
     }
     return $this;
 }