-
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.
initial commit that implements core functionality
- Loading branch information
1 parent
352d026
commit ae0edcb
Showing
11 changed files
with
596 additions
and
0 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,143 @@ | ||
<?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 Config extends \Magento\Framework\App\Helper\AbstractHelper { | ||
|
||
/** | ||
* The URL parameter we should look for to set the current coupon code. | ||
* | ||
* @var DEFAULT_URL_PARAMETER URL parameter containing the coupon code | ||
*/ | ||
public const DEFAULT_URL_PARAMETER = 'coupon'; | ||
|
||
/** | ||
* When a code is supplied via the URL, a cookie is set that allows us to | ||
* remember it during a session. | ||
* | ||
* @var COUPON_COOKIE_NAME Name of cookie that stores discount code | ||
*/ | ||
public const COUPON_COOKIE_NAME = 'discount_coupon_url_code'; | ||
|
||
/** | ||
* This is how long a browser session should remember the last coupon code | ||
* that was supplied via the URL in seconds. A default value of 0 means | ||
* the cookie will last as long as the session (i.e. until the browser tab | ||
* or window is closed.) | ||
* | ||
* @var DEFAULT_COOKIE_LIFETIME Default cookie lifetime in seconds | ||
*/ | ||
public const DEFAULT_COOKIE_LIFETIME = 0; | ||
|
||
/** | ||
* @var ENABLED_CONFIG_PATH Whether or not the module is enabled | ||
*/ | ||
public const ENABLED_CONFIG_PATH = 'promo/discounturl/enabled'; | ||
|
||
/** | ||
* @var URL_PARAMETER_CONFIG_PATH GET parameter that should set the coupon code | ||
*/ | ||
public const URL_PARAMETER_CONFIG_PATH = 'promo/discounturl/url_param'; | ||
|
||
/** | ||
* @var COOKIE_LIFETIME_CONFIG_PATH How long the cookie should last | ||
*/ | ||
public const COOKIE_LIFETIME_CONFIG_PATH = 'promo/discounturl/cookie_lifetime'; | ||
|
||
/** | ||
* @var \Magento\Framework\App\Config\ScopeConfigInterface | ||
*/ | ||
public $scopeConfig; | ||
|
||
/************************************************************************/ | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param \Magento\Framework\App\Helper\Context $context | ||
* @param \Magento\Framework\Module\ModuleListInterface $moduleList | ||
*/ | ||
public function __construct( | ||
\Magento\Framework\App\Helper\Context $context, | ||
\Magento\Framework\Module\ModuleListInterface $moduleList | ||
) { | ||
$this->scopeConfig = $context->getScopeConfig(); | ||
parent::__construct($context); | ||
} | ||
|
||
/************************************************************************/ | ||
|
||
/** | ||
* Returns whether or not the module is enabled. | ||
* | ||
* @param string|int $scope | ||
* | ||
* @return bool | ||
*/ | ||
public function isEnabled($scope = 'default'): bool { | ||
|
||
$value = $this->scopeConfig->getValue( | ||
self::ENABLED_CONFIG_PATH, | ||
\Magento\Store\Model\ScopeInterface::SCOPE_STORE, | ||
$scope | ||
); | ||
|
||
return is_null($value) || '0' == $value ? false : true; | ||
} | ||
|
||
/************************************************************************/ | ||
|
||
/** | ||
* Returns whether or not the module is enabled. | ||
* | ||
* @param string|int $scope | ||
* | ||
* @return string | ||
*/ | ||
public function getUrlParameter($scope = 'default'): string { | ||
|
||
$value = $this->scopeConfig->getValue( | ||
self::URL_PARAMETER_CONFIG_PATH, | ||
\Magento\Store\Model\ScopeInterface::SCOPE_STORE, | ||
$scope | ||
); | ||
|
||
return is_null($value) || '' === $value ? self::DEFAULT_URL_PARAMETER : $value; | ||
} | ||
|
||
/************************************************************************/ | ||
|
||
/** | ||
* Returns whether or not the module is enabled. | ||
* | ||
* @param string|int $scope | ||
* | ||
* @return int | ||
*/ | ||
public function getCookieLifetime($scope = 'default'): int { | ||
|
||
$value = $this->scopeConfig->getValue( | ||
self::COOKIE_LIFETIME_CONFIG_PATH, | ||
\Magento\Store\Model\ScopeInterface::SCOPE_STORE, | ||
$scope | ||
); | ||
|
||
return is_null($value) || '' === $value ? self::DEFAULT_COOKIE_LIFETIME : $value; | ||
} | ||
|
||
/************************************************************************/ | ||
|
||
public function getCookieName(): string { | ||
|
||
return self::COUPON_COOKIE_NAME; | ||
} | ||
} | ||
|
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,109 @@ | ||
<?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 Cookie { | ||
|
||
/** | ||
* @var \Magento\Framework\Stdlib\CookieManagerInterface | ||
*/ | ||
private $cookieManager; | ||
|
||
/** | ||
* @var \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory | ||
*/ | ||
private $cookieMetadataFactory; | ||
|
||
/** | ||
* @var \Magento\Framework\Session\SessionManagerInterface | ||
*/ | ||
private $sessionManager; | ||
|
||
/** | ||
* @var \Crankycyclops\DiscountCodeUrl\Helper\Config | ||
*/ | ||
private $config; | ||
|
||
/************************************************************************/ | ||
|
||
public function __construct( | ||
\Magento\Framework\Stdlib\CookieManagerInterface $cookieManager, | ||
\Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory, | ||
\Magento\Framework\Session\SessionManagerInterface $sessionManager, | ||
\Crankycyclops\DiscountCodeUrl\Helper\Config $config | ||
) { | ||
$this->cookieManager = $cookieManager; | ||
$this->cookieMetadataFactory = $cookieMetadataFactory; | ||
$this->sessionManager = $sessionManager; | ||
$this->config = $config; | ||
} | ||
|
||
/************************************************************************/ | ||
|
||
/** | ||
* Gets the session's currently applied coupon code or an empty . | ||
* | ||
* @return string|null | ||
*/ | ||
public function getCookie(): ?string { | ||
|
||
$value = $this->cookieManager->getCookie($this->config->getCookieName()); | ||
return $value ? $value : null; | ||
} | ||
|
||
/************************************************************************/ | ||
|
||
/** | ||
* Sets the coupon code cookie so we can remember it in the current session. | ||
* | ||
* @param $value | ||
* | ||
* @return void | ||
*/ | ||
public function setCookie($value): void { | ||
|
||
$cookieLifetime = $this->config->getCookieLifetime(); | ||
$metadata = $this->cookieMetadataFactory->createPublicCookieMetadata(); | ||
|
||
$metadata->setPath($this->sessionManager->getCookiePath()); | ||
$metadata->setDomain($this->sessionManager->getCookieDomain()); | ||
$metadata->setHttpOnly(false); | ||
|
||
if ($cookieLifetime > 0) { | ||
$metadata->setDuration(3600); | ||
} | ||
|
||
$this->cookieManager->setPublicCookie( | ||
$this->config->getCookieName(), | ||
$value, | ||
$metadata | ||
); | ||
} | ||
|
||
/************************************************************************/ | ||
|
||
/** | ||
* Deletes the coupon code cookie. | ||
* | ||
* @return void | ||
*/ | ||
public function deleteCookie(): void { | ||
|
||
$metadata = $this->cookieMetadataFactory->createPublicCookieMetadata(); | ||
|
||
$metadata->setPath($this->sessionManager->getCookiePath()); | ||
$metadata->setDomain($this->sessionManager->getCookieDomain()); | ||
$metadata->setHttpOnly(false); | ||
|
||
$this->cookieManager->deleteCookie($this->config->getCookieName(), $metadata); | ||
} | ||
} | ||
|
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,68 @@ | ||
<?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\Observer\Discountcodeurl; | ||
|
||
class CheckoutCartSaveAfter implements \Magento\Framework\Event\ObserverInterface { | ||
|
||
/** | ||
* @var \Magento\Quote\Api\CartRepositoryInterface | ||
*/ | ||
private $quoteRepository; | ||
|
||
/** | ||
* @var \Crankycyclops\DiscountCodeUrl\Helper\Cookie | ||
*/ | ||
private $cookieHelper; | ||
|
||
/************************************************************************/ | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param \Crankycyclops\DiscountCodeUrl\Helper\Cookie $cookieHelper | ||
*/ | ||
public function __construct( | ||
\Magento\Quote\Api\CartRepositoryInterface $quoteRepository, | ||
\Crankycyclops\DiscountCodeUrl\Helper\Cookie $cookieHelper | ||
) { | ||
$this->quoteRepository = $quoteRepository; | ||
$this->cookieHelper = $cookieHelper; | ||
} | ||
|
||
/************************************************************************/ | ||
|
||
/** | ||
* If a coupon code was set in the URL at any point during the session, | ||
* apply it as soon as the cart is created. | ||
* | ||
* @param \Magento\Framework\Event\Observer $observer | ||
* | ||
* @return void | ||
*/ | ||
public function execute(\Magento\Framework\Event\Observer $observer): void { | ||
|
||
if ($this->config->isEnabled()) { | ||
|
||
$coupon = $this->cookieHelper->getCookie(); | ||
|
||
if ($coupon) { | ||
|
||
$cart = $observer->getData('cart'); | ||
|
||
if ($cart) { | ||
$cart->getQuote()->setCouponCode($coupon); | ||
$this->quoteRepository->save($cart->getQuote()->collectTotals()); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
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,56 @@ | ||
<?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\Observer\Discountcodeurl; | ||
|
||
class SalesOrderPlaceAfter implements \Magento\Framework\Event\ObserverInterface { | ||
|
||
/** | ||
* @var \Crankycyclops\DiscountCodeUrl\Helper\Cookie | ||
*/ | ||
private $cookieHelper; | ||
|
||
/************************************************************************/ | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param \Crankycyclops\DiscountCodeUrl\Helper\Cookie $cookieHelper | ||
*/ | ||
public function __construct( | ||
\Crankycyclops\DiscountCodeUrl\Helper\Cookie $cookieHelper | ||
) { | ||
$this->cookieHelper = $cookieHelper; | ||
} | ||
|
||
/************************************************************************/ | ||
|
||
/** | ||
* If a coupon code was set in the URL at any point during the session | ||
* an an order was successfully placed, we should remove it to avoid | ||
* having it get automatically applied a secon time (user will have to | ||
* either enter the code manually again or browse once more to the | ||
* coupon-specific URL.) | ||
* | ||
* @param \Magento\Framework\Event\Observer $observer | ||
* | ||
* @return void | ||
*/ | ||
public function execute(\Magento\Framework\Event\Observer $observer): void { | ||
|
||
// Once we've placed an order, we should delete the coupon cookie so | ||
// that the user will have to add one again if they wish to place | ||
// another order | ||
if ($this->config->isEnabled()) { | ||
$this->cookieHelper->deleteCookie(); | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.