*/ class Mage_Checkout_Model_Cart extends Varien_Object { protected $_summaryQty = null; protected $_productIds = null; /** * Montant max du panier */ protected $_CartMaxAmount = '400'; /** * Get shopping cart resource model */ protected function _getResource() { return Mage::getResourceSingleton('checkout/cart'); } /** * Retrieve checkout session model * * @return Mage_Checkout_Model_Session */ public function getCheckoutSession() { return Mage::getSingleton('checkout/session'); } /** * Retrieve custome session model * * @return Mage_Customer_Model_Customer */ public function getCustomerSession() { return Mage::getSingleton('customer/session'); } public function getItems() { if (!$this->getQuote()->getId()) { return array(); } return $this->getQuote()->getItemsCollection(); } /** * Retrieve array of cart product ids * * @return array */ public function getQuoteProductIds() { $products = $this->getData('product_ids'); if (is_null($products)) { $products = array(); foreach ($this->getQuote()->getAllItems() as $item) { $products[$item->getProductId()] = $item->getProductId(); } $this->setData('product_ids', $products); } return $products; } /** * Get quote object associated with cart. By default it is current customer session quote * * @return Mage_Sales_Model_Quote */ public function getQuote() { if (!$this->hasData('quote')) { $this->setData('quote', $this->getCheckoutSession()->getQuote()); } return $this->_getData('quote'); } /** * Initialize cart quote state to be able use it on cart page */ public function init() { $this->getQuote()->setCheckoutMethod(''); /** * If user try do checkout, reset shipiing and payment data */ if ($this->getCheckoutSession()->getCheckoutState() !== Mage_Checkout_Model_Session::CHECKOUT_STATE_BEGIN) { $this->getQuote() ->removeAllAddresses() ->removePayment(); $this->getCheckoutSession()->resetCheckout(); } if (!$this->getQuote()->hasItems()) { $this->getQuote()->getShippingAddress() ->setCollectShippingRates(false) ->removeAllShippingRates(); } return $this; } /** * Convert order item to quote item * * @param Mage_Sales_Model_Order_Item $orderItem * @param mixed $qtyFlag if is null set product qty like in order * @return Mage_Checkout_Model_Cart */ public function addOrderItem($orderItem, $qtyFlag=null) { /* @var $orderItem Mage_Sales_Model_Order_Item */ if (is_null($orderItem->getParentItem())) { $product = Mage::getModel('catalog/product') ->setStoreId(Mage::app()->getStore()->getId()) ->load($orderItem->getProductId()); if (!$product->getId()) { return $this; } $info = $orderItem->getProductOptionByCode('info_buyRequest'); $info = new Varien_Object($info); if (is_null($qtyFlag)) { $info->setQty($orderItem->getQtyOrdered()); } else { $info->setQty(1); } $this->addProduct($product, $info); } return $this; } /** * Get product object based on requested product information * * @param mixed $productInfo * @return Mage_Catalog_Model_Product */ protected function _getProduct($productInfo) { if ($productInfo instanceof Mage_Catalog_Model_Product) { $product = $productInfo; } elseif (is_int($productInfo)) { $product = Mage::getModel('catalog/product') ->setStoreId(Mage::app()->getStore()->getId()) ->load($productInfo); } else { Mage::throwException(Mage::helper('checkout')->__('The product could not be found.')); } return $product; } /** * Get request for product add to cart procedure * * @param mixed $requestInfo * @return Varien_Object */ protected function _getProductRequest($requestInfo) { if ($requestInfo instanceof Varien_Object) { $request = $requestInfo; } elseif (is_numeric($requestInfo)) { $request = new Varien_Object(); $request->setQty($requestInfo); } else { $request = new Varien_Object($requestInfo); } if (!$request->hasQty()) { $request->setQty(1); } return $request; } /** * Add product to shopping cart (quote) * * @param int $productId * @param int $qty * @return Mage_Checkout_Model_Cart */ public function addProduct($product, $info=null) { $product = $this->_getProduct($product); $request = $this->_getProductRequest($info); //Check if current product already exists in cart $productId = $product->getId(); $items = $this->getQuote()->getAllItems(); $quoteProduct = null; foreach ($items as $item) { if ($item->getProductId() == $productId) { $quoteProduct = $item; break; } } if ($product->getStockItem()) { $minimumQty = $product->getStockItem()->getMinSaleQty(); //If product was not found in cart and there is set minimal qty for it if($minimumQty && $minimumQty > 0 && $request->getQty() < $minimumQty && $quoteProduct === null){ $request->setQty($minimumQty); } } /** * Only add product if shopping cart Amount < max cart amount */ if ( $this->_isCartAmountOk($product->getPrice()*$request->getQty())) { if ($product->getId()) { $result = $this->getQuote()->addProduct($product, $request); /** * String we can get if prepare process has error */ if (is_string($result)) { $this->getCheckoutSession()->setRedirectUrl($product->getProductUrl()); if ($this->getCheckoutSession()->getUseNotice() === null) { $this->getCheckoutSession()->setUseNotice(true); } Mage::throwException($result); } } else { Mage::throwException(Mage::helper('checkout')->__('The product does not exist.')); } Mage::dispatchEvent('checkout_cart_product_add_after', array('quote_item'=>$result, 'product'=>$product)); $this->getCheckoutSession()->setLastAddedProductId($product->getId()); } else { $this->getCheckoutSession()->addError(Mage::helper('checkout')->__('Your cart is over the cart max amount.')); } return $this; } /** * Adding products to cart by ids * * @param array $productIds * @return Mage_Checkout_Model_Cart */ public function addProductsByIds($productIds) { $allAvailable = true; $allAdded = true; if (!empty($productIds)) { foreach ($productIds as $productId) { $productId = (int) $productId; if (!$productId) { continue; } $product = Mage::getModel('catalog/product') ->setStoreId(Mage::app()->getStore()->getId()) ->load($productId); if ($product->getId() && $product->isVisibleInCatalog()) { try { //Add Only product if cart amount < max cart amount if ( $this->_isCartAmountOk($product->getPrice())) { $this->getQuote()->addProduct($product); } else { $this->getCheckoutSession()->addError(Mage::helper('checkout')->__('%s was not added to your cart. Because your cart amount is over the allowed max amount.',$product->getName())); } } catch (Exception $e){ $allAdded = false; } } else { $allAvailable = false; } } if (!$allAvailable) { $this->getCheckoutSession()->addError( Mage::helper('checkout')->__('Some of the requested products are unavailable.') ); } if (!$allAdded) { $this->getCheckoutSession()->addError( Mage::helper('checkout')->__('Some of the requested products are not available in the desired quantity.') ); } } return $this; } /** * Update cart items information * * @param array $data * @return Mage_Checkout_Model_Cart */ public function updateItems($data) { Mage::dispatchEvent('checkout_cart_update_items_before', array('cart'=>$this, 'info'=>$data)); //Variable pour afficher une seule fois le message d'erreur $MessageDisplayed = false; foreach ($data as $itemId => $itemInfo) { $item = $this->getQuote()->getItemById($itemId); if (!$item) { continue; } if (!empty($itemInfo['remove']) || (isset($itemInfo['qty']) && $itemInfo['qty']=='0')) { $this->removeItem($itemId); continue; } $qty = isset($itemInfo['qty']) ? (float) $itemInfo['qty'] : false; /* Récupération du prix de l'item */ $product = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId()) ->load($item->getProductId()); $productPrice = $product->getPrice(); $productQty = isset($qty) ? (float) $qty - $item->getQty() : false ; if ( $this->_isCartAmountOk( $productPrice * $productQty)) { if ($qty > 0) { $item->setQty($qty); } Mage::dispatchEvent('checkout_cart_update_items_after', array('cart'=>$this, 'info'=>$data)); } else { if ( !$MessageDisplayed ) { $this->getCheckoutSession()->addError(Mage::helper('checkout')->__('Your cart is over the cart max amount.')); $MessageDisplayed = true; } } } return $this; } /** * Remove item from cart * * @param int $itemId * @return Mage_Checkout_Model_Cart */ public function removeItem($itemId) { $this->getQuote()->removeItem($itemId); return $this; } /** * Save cart * * @return Mage_Checkout_Model_Cart */ public function save() { $this->getQuote()->getBillingAddress(); $this->getQuote()->getShippingAddress()->setCollectShippingRates(true); $this->getQuote()->collectTotals(); $this->getQuote()->save(); $this->getCheckoutSession()->setQuoteId($this->getQuote()->getId()); /** * Cart save usually called after chenges with cart items. */ Mage::dispatchEvent('checkout_cart_save_after', array('cart'=>$this)); return $this; } public function truncate() { foreach ($this->getQuote()->getItemsCollection() as $item) { $item->isDeleted(true); } } public function getProductIds() { $quoteId = Mage::getSingleton('checkout/session')->getQuoteId(); if (null === $this->_productIds) { $this->_productIds = array(); if ($this->getSummaryQty()>0) { foreach ($this->getQuote()->getAllItems() as $item) { $this->_productIds[] = $item->getProductId(); } } $this->_productIds = array_unique($this->_productIds); } return $this->_productIds; } /** * Get shopping cart items summary (inchlude config settings) * * @return decimal */ public function getSummaryQty() { $quoteId = Mage::getSingleton('checkout/session')->getQuoteId(); //If there is no quote id in session trying to load quote //and get new quote id. This is done for cases when quote was created //not by customer (from backend for example). if (!$quoteId && Mage::getSingleton('customer/session')->isLoggedIn()) { $quote = Mage::getSingleton('checkout/session')->getQuote(); $quoteId = Mage::getSingleton('checkout/session')->getQuoteId(); } if ($quoteId && $this->_summaryQty === null) { if (Mage::getStoreConfig('checkout/cart_link/use_qty')) { $this->_summaryQty = $this->getItemsQty(); } else { $this->_summaryQty = $this->getItemsCount(); } } return $this->_summaryQty; } /** * Get shopping cart items count * * @return int */ public function getItemsCount() { return $this->getQuote()->getItemsCount()*1; } /** * Get shopping cart summary qty * * @return decimal */ public function getItemsQty() { return $this->getQuote()->getItemsQty()*1; } /** * Europe-Internet * Is shopping cart Amount < max cart amount ? * * @param float $productAmount * @return bool */ protected function _isCartAmountOk( $productAmount = '') { $Amount = Mage::getSingleton('checkout/cart')->getQuote()->getGrandTotal(); if ( ( $Amount + $productAmount ) <= $this->_CartMaxAmount ) { return true; } else { return false; } } }