Skip to content
This repository has been archived by the owner on Apr 6, 2020. It is now read-only.

Commit

Permalink
Adicionado modulo Trezo_LimitCartItemQty
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoroeder committed Jun 6, 2017
1 parent 3121386 commit c533441
Show file tree
Hide file tree
Showing 10 changed files with 209 additions and 2 deletions.
76 changes: 76 additions & 0 deletions Observer/Limit/CartSave.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Trezo
*
* NOTICE OF LICENSE
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.bseller.com.br for more information.
*
* @category Trezo
* @package Trezo_LimitCartItemQty
*
* @copyright Copyright (c) 2017 Trezo. (http://www.trezo.com.br)
*
* @author Bruno Roeder <bruno@trezo.com.br>
*/

namespace Trezo\LimitCartItemQty\Observer\Limit;

class CartSave implements \Magento\Framework\Event\ObserverInterface
{
protected $_checkoutSession;
protected $_scopeConfig;
protected $_logger;
protected $_messageManager;
protected $_message = null;
protected $_totalQtyInCart;

public function __construct(
\Magento\Checkout\Model\Session $checkoutSession,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Psr\Log\LoggerInterface $logger,
\Magento\Framework\Message\ManagerInterface $messageManager
) {
$this->_scopeConfig = $scopeConfig;
$this->_checkoutSession = $checkoutSession;
$this->_logger = $logger;
$this->_messageManager = $messageManager;
}

/**
* Execute observer, limiting the qty of intens in cart
*
* @param \Magento\Framework\Event\Observer $observer
* @return void
*/
public function execute(
\Magento\Framework\Event\Observer $observer
) {
if ($this->_scopeConfig->getValue(
'limitcartitemqty/configuration/enabled',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
)) {
$object = $observer->getObject();
// only cart item
if ($object instanceof \Magento\Quote\Model\Quote\Item) {
$totalQtyInCart = $object->getQuote()->getItemsQty();
$maxItemsQtyInCart = $this->_scopeConfig->getValue(
'limitcartitemqty/configuration/max_item_qty',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);

if ($totalQtyInCart > $maxItemsQtyInCart) {
$erroMessage = $this->_scopeConfig->getValue(
'limitcartitemqty/configuration/error_message',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
throw new \Magento\Framework\Exception\LocalizedException(__(sprintf($erroMessage, $maxItemsQtyInCart)));
}
}
}
}
}
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,29 @@
# module-limitcartitemqty
Magento 2 modulo to limit the qty of itens in the cart.
# Extensão Limitar Quantidade de Itens no Carrinho para Magento 2 (Magento CE 2)
Com este módulo é possível limitar a quantidade de itens que seu cliente possui no carrinho em cada compra, possibilitando que se limite uma quantidade personalizada e que quando o limite seja atingido, uma mensagem informativa é apresentada ao seu cliente (mensagem personalizada via Admin).
## Instalação
### Instalar usando o [composer](https://getcomposer.org/):

1. Entre na pasta raíz da sua instalação
2. Digite o seguinte comando:
```bash
composer require mageup/module-limitcartitemqty:dev-master
```

3. Digite os seguintes comandos, no terminal, para habilitar o módulo:

```bash
php bin/magento module:enable Trezo_LimitCartItemQty --clear-static-content
php bin/magento setup:upgrade
```
### ou baixar e instalar manualmente:


* Criar a seguinte estrutura de pastas app/code/Trezo/LimitCartItemQty
* Baixe a ultima versão [aqui](https://codeload.github.com/mageup/module-limitcartitemqty/zip/master)
* Descompacte o arquivo baixado e copie as pastas para dentro do diretório criado no início
* Digite os seguintes comandos, no terminal, para habilitar o módulo:

```bash
php bin/magento module:enable Trezo_LimitCartItemQty --clear-static-content
php bin/magento setup:upgrade
```
25 changes: 25 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "trezo/module-limitcartitemqty",
"description": "Limit the qty of itens in the cart.",
"license": "proprietary",
"authors": [
{
"name": "Trezo",
"email": "contato@trezo.com.br"
},
{
"name": "Bruno Roeder",
"email": "bruno@trezo.com.br"
}
],
"minimum-stability": "dev",
"require": {},
"autoload": {
"psr-4": {
"Trezo\\LimitCartItemQty\\": ""
},
"files": [
"registration.php"
]
}
}
16 changes: 16 additions & 0 deletions etc/acl.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
<acl>
<resources>
<resource id="Magento_Backend::admin">
<resource id="Magento_Backend::stores">
<resource id="Magento_Backend::stores_settings">
<resource id="Magento_Config::config">
<resource id="Trezo_LimitCartItemQty::config_trezo_limitcartitemqty" title="limitcartitemqty"/>
</resource>
</resource>
</resource>
</resource>
</resources>
</acl>
</config>
29 changes: 29 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<tab id="trezo" sortOrder="999" translate="label">
<label>trezo</label>
</tab>
<section id="limitcartitemqty" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label">
<label>Limit Cart Item Qty</label>
<tab>trezo</tab>
<resource>Trezo_LimitCartItemQty::config_trezo_limitcartitemqty</resource>
<group id="configuration" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label">
<label>Configuration</label>
<field id="enabled" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label" type="select">
<label>Enabled</label>
<comment/>
<source_model>Magento\Config\Model\Config\Source\Enabledisable</source_model>
</field>
<field id="max_item_qty" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="20" translate="label" type="text">
<label>Max qty item</label>
<comment/>
</field>
<field id="error_message" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="30" translate="label" type="text">
<label>Error message</label>
<comment/>
</field>
</group>
</section>
</system>
</config>
12 changes: 12 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<limitcartitemqty>
<configuration>
<enabled>0</enabled>
<max_item_qty>7</max_item_qty>
<error_message><![CDATA[The maximum quantity allowed in the cart is %s items per purchase.]]></error_message>
</configuration>
</limitcartitemqty>
</default>
</config>
6 changes: 6 additions & 0 deletions etc/events.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="model_save_before">
<observer instance="Trezo\LimitCartItemQty\Observer\Limit\CartSave" name="trezo_limitcartitemqty_observer_model_save_before"/>
</event>
</config>
4 changes: 4 additions & 0 deletions etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Trezo_LimitCartItemQty" setup_version="1.0.0"/>
</config>
6 changes: 6 additions & 0 deletions i18n/pt_BR.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"Limit Cart Item Qty","Limitar quantidade de itens no carrinho"
"Enabled","Habilitado"
"Max qty item","Quantidade máxima de itens"
"Error message","Mensagem de erro"
"The maximum quantity allowed in the cart is %s items per purchase.","A quantidade máxima permitida no carrinho é de %s itens por compra."
"Configuration","Configuração"
6 changes: 6 additions & 0 deletions registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Trezo_LimitCartItemQty',
__DIR__
);

0 comments on commit c533441

Please sign in to comment.