-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
If a cart/quote exists already, the module will now automatically app…
…ly the coupon to it as soon as a page is loaded with a valid coupon code in the URL
- Loading branch information
1 parent
c8d9399
commit 17eea81
Showing
3 changed files
with
132 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
/** | ||
* @category Crankycyclops | ||
* @package Crankycyclops_DiscountCodeUrl | ||
* @author James Colannino | ||
* @copyright Copyright (c) 2019 James Colannino | ||
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL v3 | ||
*/ | ||
|
||
namespace Crankycyclops\DiscountCodeUrl\Helper; | ||
|
||
class Cart extends \Magento\Framework\App\Helper\AbstractHelper { | ||
|
||
/** | ||
* @var \Magento\Quote\Api\CartRepositoryInterface | ||
*/ | ||
private $quoteRepository; | ||
|
||
/** | ||
* @var \Magento\Framework\Message\ManagerInterface | ||
*/ | ||
private $messageManager; | ||
|
||
/************************************************************************/ | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository | ||
* @param \Magento\Framework\Message\ManagerInterface $messageManager | ||
*/ | ||
public function __construct( | ||
\Magento\Quote\Api\CartRepositoryInterface $quoteRepository, | ||
\Magento\Framework\Message\ManagerInterface $messageManager | ||
) { | ||
$this->quoteRepository = $quoteRepository; | ||
$this->messageManager = $messageManager; | ||
} | ||
|
||
/************************************************************************/ | ||
|
||
public function applyCoupon(\Magento\Quote\Model\Quote $quote, string $coupon): void { | ||
|
||
try { | ||
$quote->setCouponCode($coupon); | ||
$this->quoteRepository->save($quote->collectTotals()); | ||
} | ||
|
||
catch (LocalizedException $e) { | ||
$this->messageManager->addError( | ||
__("Discount code <strong>$coupon</strong> couldn't be applied: " . | ||
$e->getMessage()) | ||
); | ||
} | ||
|
||
catch (\Exception $e) { | ||
$this->messageManager->addError( | ||
__("Discount code <strong>$coupon</strong> couldn't be applied or is invalid") | ||
); | ||
} | ||
|
||
if ($quote->getCouponCode() != $coupon) { | ||
$this->messageManager->addError( | ||
__("Discount code <strong>$coupon</strong> is invalid. Verify that it's correct and try again.") | ||
); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters