{"id":310,"date":"2012-04-06T08:55:51","date_gmt":"2012-04-06T06:55:51","guid":{"rendered":"http:\/\/www.h-hennes.fr\/blog\/?p=310"},"modified":"2015-06-26T10:54:29","modified_gmt":"2015-06-26T08:54:29","slug":"magento-configurer-un-montant-maximum-de-commande","status":"publish","type":"post","link":"https:\/\/www.h-hennes.fr\/blog\/2012\/04\/06\/magento-configurer-un-montant-maximum-de-commande\/","title":{"rendered":"Magento : Configurer un montant maximum de commande"},"content":{"rendered":"<p><strong>Edit 2015 :<\/strong><br \/>\nCette solution est dat\u00e9e, et n&rsquo;est pas la plus pratique.<br \/>\nPr\u00e9f\u00e9rez plut\u00f4t la solution propos\u00e9e par le lien suivant : <a href=\"http:\/\/inchoo.net\/magento\/magento-maximum-allowed-order-amount\/\">http:\/\/inchoo.net\/magento\/magento-maximum-allowed-order-amount\/<\/a><\/p>\n<p>Si pour une raison x ou y vous souhaitez limiter le montant maximum des commandes r\u00e9alis\u00e9es sur votre site ecommerce magentento, voici une solution rapide pour r\u00e9aliser cette action.<br \/>\n( cette modification a \u00e9t\u00e9 mise en place sur une version 1.4.1.1 de magento )<\/p>\n<p>Pour r\u00e9aliser cette action, nous allons ajouter une v\u00e9rification du montant du panier, avant l&rsquo;ajout de nouveaux \u00e9l\u00e9ments \u00e0 celui-ci.<br \/>\nCe comportement est g\u00e9r\u00e9 dans le mod\u00e8le du panier situ\u00e9 dans le fichier suivant app\/code\/core\/Mage\/Checkout\/Model\/Cart.php.<\/p>\n<p>Pour commencer et avant toute modification, faites une copie de ce fichier dans le dossier app\/code\/local\/Mage\/Checkout\/Model\/Cart.php<\/p>\n<p>Passons \u00e0 pr\u00e9sent au code , nous allons mettre en place une nouvelle variable qui contiendra le montant maximum du panier au format num\u00e9rique.<\/p>\n<pre lang=\"php\">\/**\r\n* Montant maximum de la commande\r\n*\/\r\nprotected $_CartMaxAmount = '400';<\/pre>\n<p>Nous cr\u00e9ons ensuite une fonction qui sera charg\u00e9e de v\u00e9rifier que le montant du panier est bien inf\u00e9rieur au montant d\u00e9fini dans notre variable pr\u00e9c\u00e9dente.<\/p>\n<pre lang=\"php\">\/**\r\n* Montant panier &lt; Montant maximum de la commande ?\r\n*\r\n* @param float $productAmount\r\n* @return bool\r\n*\/\r\nprotected function _isCartAmountOk( $productAmount = '') {\r\n\r\n$Amount = Mage::getSingleton('checkout\/cart')-&gt;getQuote()-&gt;getGrandTotal();\r\n\r\nif ( ( $Amount + $productAmount ) &lt;= $this-&gt;_CartMaxAmount ) {\r\n\r\nreturn true;\r\n}\r\n\r\nelse {\r\nreturn false;\r\n}\r\n\r\n}<\/pre>\n<p>Une fois cette fonction cr\u00e9\u00e9e, il faut maintenant rajouter cette v\u00e9rification dans les diff\u00e9rents cas de modification du panier :<\/p>\n<ul>\n<li>Ajout d&rsquo;un produit unique au panier<\/li>\n<li>Ajout de plusieurs produits au panier<\/li>\n<li>Mise \u00e0 jour d&rsquo;un produit du panier<\/li>\n<\/ul>\n<p>Commen\u00e7ons par la fonction d&rsquo;ajout d&rsquo;un produit au panier :<\/p>\n<pre lang=\"php\">\/**\r\n* Add product to shopping cart (quote)\r\n*\r\n* @param   int $productId\r\n* @param   int $qty\r\n* @return  Mage_Checkout_Model_Cart\r\n*\/\r\npublic function addProduct($product, $info=null)\r\n{\r\n$product = $this-&gt;_getProduct($product);\r\n$request = $this-&gt;_getProductRequest($info);\r\n\r\n\/\/Check if current product already exists in cart\r\n$productId = $product-&gt;getId();\r\n$items = $this-&gt;getQuote()-&gt;getAllItems();\r\n$quoteProduct = null;\r\nforeach ($items as $item) {\r\nif ($item-&gt;getProductId() == $productId) {\r\n$quoteProduct = $item;\r\nbreak;\r\n}\r\n}\r\n\r\nif ($product-&gt;getStockItem()) {\r\n$minimumQty = $product-&gt;getStockItem()-&gt;getMinSaleQty();\r\n\/\/If product was not found in cart and there is set minimal qty for it\r\nif($minimumQty &amp;&amp; $minimumQty &gt; 0 &amp;&amp; $request-&gt;getQty() &lt; $minimumQty &amp;&amp; $quoteProduct === null){\r\n$request-&gt;setQty($minimumQty);\r\n}\r\n}\r\n\r\n\/**\r\n* Only add product if shopping cart Amount &lt; max cart amount\r\n*\/\r\nif ( $this-&gt;_isCartAmountOk($product-&gt;getPrice()*$request-&gt;getQty())) {\r\n\r\nif ($product-&gt;getId()) {\r\n$result = $this-&gt;getQuote()-&gt;addProduct($product, $request);\r\n\/**\r\n* String we can get if prepare process has error\r\n*\/\r\nif (is_string($result)) {\r\n$this-&gt;getCheckoutSession()-&gt;setRedirectUrl($product-&gt;getProductUrl());\r\nif ($this-&gt;getCheckoutSession()-&gt;getUseNotice() === null) {\r\n$this-&gt;getCheckoutSession()-&gt;setUseNotice(true);\r\n}\r\nMage::throwException($result);\r\n}\r\n}\r\nelse {\r\nMage::throwException(Mage::helper('checkout')-&gt;__('The product does not exist.'));\r\n}\r\n\r\nMage::dispatchEvent('checkout_cart_product_add_after', array('quote_item'=&gt;$result, 'product'=&gt;$product));\r\n$this-&gt;getCheckoutSession()-&gt;setLastAddedProductId($product-&gt;getId());\r\n\r\n}\r\n\r\nelse {\r\n$this-&gt;getCheckoutSession()-&gt;addError(Mage::helper('checkout')-&gt;__('Your cart is over the cart max amount.'));\r\n}\r\n\r\nreturn $this;\r\n}<\/pre>\n<p>Ensuite la fonction d&rsquo;ajout de plusieurs produits au panier :<\/p>\n<pre lang=\"php\">\/**\r\n* Adding products to cart by ids\r\n*\r\n* @param   array $productIds\r\n* @return  Mage_Checkout_Model_Cart\r\n*\/\r\npublic function addProductsByIds($productIds)\r\n{\r\n$allAvailable = true;\r\n$allAdded     = true;\r\n\r\nif (!empty($productIds)) {\r\nforeach ($productIds as $productId) {\r\n$productId = (int) $productId;\r\nif (!$productId) {\r\ncontinue;\r\n}\r\n$product = Mage::getModel('catalog\/product')\r\n-&gt;setStoreId(Mage::app()-&gt;getStore()-&gt;getId())\r\n-&gt;load($productId);\r\nif ($product-&gt;getId() &amp;&amp; $product-&gt;isVisibleInCatalog()) {\r\ntry {\r\n\r\n\/\/Add Only product if cart amount &lt; max cart amount\r\nif ( $this-&gt;_isCartAmountOk($product-&gt;getPrice())) {\r\n$this-&gt;getQuote()-&gt;addProduct($product);\r\n}\r\nelse {\r\n$this-&gt;getCheckoutSession()-&gt;addError(Mage::helper('checkout')-&gt;__('%s was not added to your cart. Because your cart amount is over the allowed max amount.',$product-&gt;getName()));\r\n}\r\n} catch (Exception $e){\r\n$allAdded = false;\r\n}\r\n} else {\r\n$allAvailable = false;\r\n}\r\n}\r\n\r\nif (!$allAvailable) {\r\n$this-&gt;getCheckoutSession()-&gt;addError(\r\nMage::helper('checkout')-&gt;__('Some of the requested products are unavailable.')\r\n);\r\n}\r\nif (!$allAdded) {\r\n$this-&gt;getCheckoutSession()-&gt;addError(\r\nMage::helper('checkout')-&gt;__('Some of the requested products are not available in the desired quantity.')\r\n);\r\n}\r\n}\r\nreturn $this;\r\n}<\/pre>\n<p>Puis pour finir la fonction de mise \u00e0 jour :<\/p>\n<pre lang=\"php\">\/**\r\n* Update cart items information\r\n*\r\n* @param   array $data\r\n* @return  Mage_Checkout_Model_Cart\r\n*\/\r\npublic function updateItems($data)\r\n{\r\nMage::dispatchEvent('checkout_cart_update_items_before', array('cart'=&gt;$this, 'info'=&gt;$data));\r\n\r\n\/\/Variable pour afficher une seule fois le message d'erreur\r\n$MessageDisplayed = false;\r\n\r\nforeach ($data as $itemId =&gt; $itemInfo) {\r\n$item = $this-&gt;getQuote()-&gt;getItemById($itemId);\r\nif (!$item) {\r\ncontinue;\r\n}\r\n\r\nif (!empty($itemInfo['remove']) || (isset($itemInfo['qty']) &amp;&amp; $itemInfo['qty']=='0')) {\r\n$this-&gt;removeItem($itemId);\r\ncontinue;\r\n}\r\n\r\n$qty = isset($itemInfo['qty']) ? (float) $itemInfo['qty'] : false;\r\n\r\n\/* R\u00e9cup\u00e9ration du prix de l'item *\/\r\n$product =  Mage::getModel('catalog\/product')-&gt;setStoreId(Mage::app()-&gt;getStore()-&gt;getId())\r\n-&gt;load($item-&gt;getProductId());\r\n\r\n$productPrice = $product-&gt;getPrice();\r\n$productQty = isset($qty) ? (float) $qty - $item-&gt;getQty() : false ;\r\n\r\nif ( $this-&gt;_isCartAmountOk( $productPrice * $productQty)) {\r\n\r\nif ($qty &gt; 0) {\r\n$item-&gt;setQty($qty);\r\n}\r\n\r\nMage::dispatchEvent('checkout_cart_update_items_after', array('cart'=&gt;$this, 'info'=&gt;$data));\r\n}\r\n\r\nelse {\r\nif ( !$MessageDisplayed ) {\r\n\r\n$this-&gt;getCheckoutSession()-&gt;addError(Mage::helper('checkout')-&gt;__('Your cart is over the cart max amount.'));\r\n$MessageDisplayed = true;\r\n}\r\n}\r\n\r\n}\r\n\r\nreturn $this;\r\n}<\/pre>\n<p>Une fois l&rsquo;ensemble des ces modifications effectu\u00e9e, notre limite de panier est en place.<br \/>\nLe fichier modifi\u00e9 est disponible ici : <a href=\"https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2012\/04\/cart.txt\" target=\"_blank\">cart<\/a><\/p>\n<p>Ce concept est juste un petit exemple peut \u00eatre \u00e9galement am\u00e9lior\u00e9 en stockant le montant du panier maximum dans une variable de configuration par exemple \ud83d\ude09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Edit 2015 : Cette solution est dat\u00e9e, et n&rsquo;est pas la plus pratique. Pr\u00e9f\u00e9rez plut\u00f4t la solution propos\u00e9e par le lien suivant : http:\/\/inchoo.net\/magento\/magento-maximum-allowed-order-amount\/ Si pour une raison x ou y vous souhaitez limiter le montant maximum des commandes r\u00e9alis\u00e9es sur votre site ecommerce magentento, voici une solution rapide pour r\u00e9aliser cette action. ( cette [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[246,6],"tags":[150,207],"class_list":["post-310","post","type-post","status-publish","format-standard","hentry","category-magento-2","category-trucs-et-astuces","tag-magento","tag-panier-commande"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts\/310","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/comments?post=310"}],"version-history":[{"count":7,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts\/310\/revisions"}],"predecessor-version":[{"id":1021,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts\/310\/revisions\/1021"}],"wp:attachment":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/media?parent=310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/categories?post=310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/tags?post=310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}