diff --git a/Helper/Config.php b/Helper/Config.php new file mode 100644 index 0000000..16bebc9 --- /dev/null +++ b/Helper/Config.php @@ -0,0 +1,143 @@ +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; + } +} + diff --git a/Helper/Cookie.php b/Helper/Cookie.php new file mode 100644 index 0000000..a354d92 --- /dev/null +++ b/Helper/Cookie.php @@ -0,0 +1,109 @@ +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); + } +} + diff --git a/Observer/Discountcodeurl/CheckoutCartSaveAfter.php b/Observer/Discountcodeurl/CheckoutCartSaveAfter.php new file mode 100644 index 0000000..7bcef41 --- /dev/null +++ b/Observer/Discountcodeurl/CheckoutCartSaveAfter.php @@ -0,0 +1,68 @@ +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()); + } + } + } + } +} + diff --git a/Observer/Discountcodeurl/SalesOrderPlaceAfter.php b/Observer/Discountcodeurl/SalesOrderPlaceAfter.php new file mode 100644 index 0000000..aee3f8d --- /dev/null +++ b/Observer/Discountcodeurl/SalesOrderPlaceAfter.php @@ -0,0 +1,56 @@ +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(); + } + } +} + diff --git a/Plugin/Framework/App/FrontControllerInterface.php b/Plugin/Framework/App/FrontControllerInterface.php new file mode 100644 index 0000000..83e1de0 --- /dev/null +++ b/Plugin/Framework/App/FrontControllerInterface.php @@ -0,0 +1,70 @@ +request = $request; + $this->config = $config; + $this->cookieHelper = $cookieHelper; + } + + /************************************************************************/ + + /** + * If coupon code is provided by the URL, remember it for the duration of + * the session. + * + * @param \Magento\Framework\App\FrontControllerInterface $subject (not used) + * + * @return void + */ + public function beforeDispatch(\Magento\Framework\App\FrontControllerInterface $subject): void { + + if ($this->config->isEnabled()) { + + $coupon = $this->request->getParam($this->config->getUrlParameter()); + + if ($coupon) { + $this->cookieHelper->setCookie($coupon); + } + } + } +} + diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml new file mode 100644 index 0000000..cec6d7e --- /dev/null +++ b/etc/adminhtml/system.xml @@ -0,0 +1,45 @@ + + + + + + + + +
+ + + + + + + + If the discount URL module is enabled, you'll be able to apply discount codes automatically via URL. Disable this if you have other modules running that implement similar behavior or that might conflict. + required-entry + Magento\Config\Model\Config\Source\Yesno + + + + + This is the GET parameter that will contain a coupon code in the URL. For example, if this value is "coupon", then https://store.url?coupon=CODE will set the coupon code to "CODE." If this value isn't set, we'll use the hardcoded default specified by Helper\Cookie::DEFAULT_URL_PARAMETER. + + + + + When a coupon code comes in through a URL, we set it in a cookie so that we can remember it for the entire session. This value determines how long that cookie should stay set before it expires (in seconds.) Value must be 0 or greater (0 means the cookie will remain alive until the browser window or tab remains open.) If this value isn't set, we'll use the hardcoded default specified by Helper\Cookie::COOKIE_LIFETIME. + validate-zero-or-greater + + + + +
+ +
+ +
diff --git a/etc/config.xml b/etc/config.xml new file mode 100644 index 0000000..59e00e8 --- /dev/null +++ b/etc/config.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + 0 + coupon + 1 + + + + + + + + + diff --git a/etc/di.xml b/etc/di.xml new file mode 100644 index 0000000..a8257d4 --- /dev/null +++ b/etc/di.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + diff --git a/etc/events.xml b/etc/events.xml new file mode 100644 index 0000000..abfe611 --- /dev/null +++ b/etc/events.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + diff --git a/etc/module.xml b/etc/module.xml new file mode 100644 index 0000000..90695bb --- /dev/null +++ b/etc/module.xml @@ -0,0 +1,16 @@ + + + + + + + + + + diff --git a/registration.php b/registration.php new file mode 100644 index 0000000..592ea74 --- /dev/null +++ b/registration.php @@ -0,0 +1,16 @@ +