From 69c5b44edbe4138d0db8577ae65033a788abf8f5 Mon Sep 17 00:00:00 2001 From: Callan Zimmermann Date: Mon, 1 Aug 2022 09:49:09 +0100 Subject: [PATCH 1/3] 2-button-disabling: disable button while loading --- view/frontend/templates/hyva/script/addtocart.phtml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/view/frontend/templates/hyva/script/addtocart.phtml b/view/frontend/templates/hyva/script/addtocart.phtml index 3c77bc9..2c59c8e 100644 --- a/view/frontend/templates/hyva/script/addtocart.phtml +++ b/view/frontend/templates/hyva/script/addtocart.phtml @@ -46,6 +46,7 @@ use Magento\Framework\Escaper; button.prepend(loader); button.classList.add('relative', 'is-loading'); + button.disabled = true; fetch(e.currentTarget.action, { method: 'POST', @@ -95,6 +96,7 @@ use Magento\Framework\Escaper; setTimeout(() => { loader.remove(); button.classList.remove('is-loading'); + button.disabled = false; messages.forEach(message => { setMessage(message); From c918473fd92f8ae3853de7d01f3d4ca6a8d514db Mon Sep 17 00:00:00 2001 From: Callan Zimmermann Date: Mon, 1 Aug 2022 10:36:44 +0100 Subject: [PATCH 2/3] 4-change-loader-timeout: add delay config and necessary blocks --- Block/AddToCart.php | 63 +++++++++++++++++++ Model/Config.php | 59 +++++++++++++++++ Model/ConfigInterface.php | 34 ++++++++++ etc/adminhtml/system.xml | 12 ++++ etc/config.xml | 1 + etc/di.xml | 11 ++++ view/frontend/layout/hyva_default.xml | 1 + .../templates/hyva/script/addtocart.phtml | 7 ++- 8 files changed, 185 insertions(+), 3 deletions(-) create mode 100644 Block/AddToCart.php create mode 100644 Model/Config.php create mode 100644 Model/ConfigInterface.php create mode 100644 etc/di.xml diff --git a/Block/AddToCart.php b/Block/AddToCart.php new file mode 100644 index 0000000..efff88d --- /dev/null +++ b/Block/AddToCart.php @@ -0,0 +1,63 @@ +config = $config; + } + + /** + * Render block HTML + * + * @return string + */ + protected function _toHtml(): string + { + if (!$this->getTemplate() || !$this->config->isAjaxAddToCartEnabled()) { + return ''; + } + return $this->fetchView($this->getTemplateFile()); + } + + /** + * Get Ajax Add To Cart Delay (in milliseconds) + * + * @return string + */ + public function getDelay(): string + { + return $this->config->getAjaxAddToCartDelay(); + } +} diff --git a/Model/Config.php b/Model/Config.php new file mode 100644 index 0000000..0b1b0a6 --- /dev/null +++ b/Model/Config.php @@ -0,0 +1,59 @@ +scopeConfig = $scopeConfig; + } + + /** + * Is the AJAX add to cart option enabled? + * + * @param null|int|string $scopeCode + * @return bool + */ + public function isAjaxAddToCartEnabled($scopeCode = null): bool + { + return (bool)$this->scopeConfig->isSetFlag( + self::XML_PATH_ENABLE_AJAX_ADD_TO_CART, + ScopeInterface::SCOPE_STORE, + $scopeCode + ); + } + + /** + * Get Ajax Add To Cart Delay + * + * @param null|int|string $scopeCode + * @return string + */ + public function getAjaxAddToCartDelay($scopeCode = null): string + { + return (string)$this->scopeConfig->getValue( + self::XML_PATH_AJAX_ADD_TO_CART_DELAY, + ScopeInterface::SCOPE_STORE, + $scopeCode + ); + } +} diff --git a/Model/ConfigInterface.php b/Model/ConfigInterface.php new file mode 100644 index 0000000..8b4d0f5 --- /dev/null +++ b/Model/ConfigInterface.php @@ -0,0 +1,34 @@ +Enable AJAX Add to Cart Magento\Config\Model\Config\Source\Yesno + + + e.g. 1000 (in milliseconds) + validate-number required-entry + diff --git a/etc/config.xml b/etc/config.xml index b228da9..57635f1 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -11,6 +11,7 @@ 0 + 1000 diff --git a/etc/di.xml b/etc/di.xml new file mode 100644 index 0000000..519c699 --- /dev/null +++ b/etc/di.xml @@ -0,0 +1,11 @@ + + + + + diff --git a/view/frontend/layout/hyva_default.xml b/view/frontend/layout/hyva_default.xml index a5f92a4..1b30caf 100644 --- a/view/frontend/layout/hyva_default.xml +++ b/view/frontend/layout/hyva_default.xml @@ -9,6 +9,7 @@ diff --git a/view/frontend/templates/hyva/script/addtocart.phtml b/view/frontend/templates/hyva/script/addtocart.phtml index 3c77bc9..5fef55d 100644 --- a/view/frontend/templates/hyva/script/addtocart.phtml +++ b/view/frontend/templates/hyva/script/addtocart.phtml @@ -5,11 +5,11 @@ */ declare(strict_types=1); -use Magento\Framework\View\Element\Template; +use Monsoon\HyvaAjaxAddToCart\Block\AddToCart; use Magento\Framework\Escaper; /** - * @var Template $block + * @var AddToCart $block * @var Escaper $escaper */ @@ -24,6 +24,7 @@ use Magento\Framework\Escaper;