Skip to content

Commit

Permalink
Added option to keep guest items in shopping cart
Browse files Browse the repository at this point in the history
  • Loading branch information
ihorvansach committed Sep 1, 2018
1 parent bd0091e commit 04a7194
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
35 changes: 29 additions & 6 deletions Model/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class Login extends \Magento\Framework\Model\AbstractModel
*/
const TIME_FRAME = 60;

const XML_PATH_KEEP_GUEST_CART = 'mfloginascustomer/general/keep_guest_cart';

/**
* Prefix of model events names
*
Expand Down Expand Up @@ -69,6 +71,11 @@ class Login extends \Magento\Framework\Model\AbstractModel
*/
protected $cart;

/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
protected $scopeConfig;

/**
* Initialize dependencies.
*
Expand All @@ -83,6 +90,7 @@ class Login extends \Magento\Framework\Model\AbstractModel
* @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
* @param array $data
* @param null|\Magento\Checkout\Model\Session $checkoutSession
* @param null|\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
*/
public function __construct(
\Magento\Framework\Model\Context $context,
Expand All @@ -95,17 +103,24 @@ public function __construct(
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = [],
$checkoutSession = null
$checkoutSession = null,
$scopeConfig = null
) {
$this->_customerFactory = $customerFactory;
$this->_customerSession = $customerSession;
$this->_checkoutSession = $checkoutSession;
$this->_dateTime = $dateTime;
$this->_random = $random;
$this->cart = $cart;
$this->_checkoutSession = $checkoutSession ?: \Magento\Framework\App\ObjectManager::getInstance()->get(

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$this->_checkoutSession = $checkoutSession ?: $objectManager->get(
\Magento\Checkout\Model\Session::class
);
$this->scopeConfig = $scopeConfig ?: $objectManager->get(
\Magento\Framework\App\Config\ScopeConfigInterface::class
);

parent::__construct($context, $registry, $resource, $resourceCollection, $data);
}

Expand Down Expand Up @@ -182,11 +197,19 @@ public function authenticateCustomer()
/* Logout if logged in */
$this->_customerSession->logout();
} else {
/* Remove items from guest cart */
foreach ($this->cart->getQuote()->getAllVisibleItems() as $item) {
$this->cart->removeItem($item->getId());

$keepItems = $this->scopeConfig->getValue(
self::XML_PATH_KEEP_GUEST_CART,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);

if (!$keepItems) {
/* Remove items from guest cart */
foreach ($this->cart->getQuote()->getAllVisibleItems() as $item) {
$this->cart->removeItem($item->getId());
}
$this->cart->save();
}
$this->cart->save();
}

$customer = $this->getCustomer();
Expand Down
6 changes: 6 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
<field id="disable_page_cache" translate="label comment" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Disable Page Cache For Admin User</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment><![CDATA[If set to "Yes", when login as customer Page Cache will be disabled.]]></comment>
</field>
<field id="keep_guest_cart" translate="label comment" type="select" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Keep Guest Shopping Cart Items</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment><![CDATA[If set to "Yes", after login, guest shopping cart will be merged into customer shopping cart.]]></comment>
</field>
</group>
</section>
Expand Down

0 comments on commit 04a7194

Please sign in to comment.