Skip to content

Commit

Permalink
Merge pull request #6 from monsoonconsulting/dev
Browse files Browse the repository at this point in the history
Merge dev into main
  • Loading branch information
callanzimmermann authored Aug 3, 2022
2 parents 06cda42 + f8e53db commit fc27f6c
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 3 deletions.
63 changes: 63 additions & 0 deletions Block/AddToCart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* Copyright © Monsoon Consulting. All rights reserved.
* See LICENSE_MONSOON.txt for license details.
*/

declare(strict_types=1);

namespace Monsoon\HyvaAjaxAddToCart\Block;

use Magento\Framework\View\Element\Template;
use Monsoon\HyvaAjaxAddToCart\Model\ConfigInterface;

/**
* Class AddToCart
*/
class AddToCart extends Template
{
/**
* @var ConfigInterface
*/
private $config;

/**
* Upsell block contructor
*
* @param Template\Context $context
* @param ConfigInterface $config
* @param array $data
*/
public function __construct(
Template\Context $context,
ConfigInterface $config,
array $data = []
) {
parent::__construct($context, $data);

$this->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();
}
}
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Changelog
<!-- Refer to: https://keepachangelog.com/en/1.0.0/ -->

## [1.1.1] 2022-03-08
### Fix
- Stop button click while loading
### Added
- Added ability to set delay in backend

## [1.1.0] 2022-22-04
### Added
- Add Admin Config Controls
Expand Down
59 changes: 59 additions & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Copyright © Monsoon Consulting. All rights reserved.
* See LICENSE_MONSOON.txt for license details
*/
declare(strict_types=1);

namespace Monsoon\HyvaAjaxAddToCart\Model;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;

/**
* Class Config
*/
class Config implements ConfigInterface
{
/** @var ScopeConfigInterface */
private ScopeConfigInterface $scopeConfig;

/**
* @param ScopeConfigInterface $scopeConfig
*/
public function __construct(
ScopeConfigInterface $scopeConfig
) {
$this->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
);
}
}
34 changes: 34 additions & 0 deletions Model/ConfigInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Copyright © Monsoon Consulting. All rights reserved.
* See LICENSE_MONSOON.txt for license details
*/
declare(strict_types=1);

namespace Monsoon\HyvaAjaxAddToCart\Model;

/**
* Interface ConfigInterface
*/
interface ConfigInterface
{
public const XML_PATH_ENABLE_AJAX_ADD_TO_CART = 'checkout/options/enable_ajax_add_to_cart';
public const XML_PATH_AJAX_ADD_TO_CART_DELAY = 'checkout/options/ajax_add_to_cart_delay';

/**
* Is the AJAX add to cart option enabled?
*
* @param null|int|string $scopeCode
* @return bool
*/

public function isAjaxAddToCartEnabled($scopeCode = null): bool;

/**
* Get Ajax Add To Cart Delay
*
* @param null|int|string $scopeCode
* @return string
*/
public function getAjaxAddToCartDelay($scopeCode = null): string;
}
12 changes: 12 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@
<label>Enable AJAX Add to Cart</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="ajax_add_to_cart_delay"
translate="label comment"
type="text"
sortOrder="110"
showInDefault="1"
showInWebsite="1"
showInStore="1"
canRestore="1">
<label>AJAX Add to Cart Delay</label>
<comment>e.g. 1000 (in milliseconds)</comment>
<validate>validate-number required-entry</validate>
</field>
</group>
</section>
</system>
Expand Down
1 change: 1 addition & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<checkout>
<options>
<enable_ajax_add_to_cart>0</enable_ajax_add_to_cart>
<ajax_add_to_cart_delay>1000</ajax_add_to_cart_delay>
</options>
</checkout>
</default>
Expand Down
11 changes: 11 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Monsoon Consulting. All rights reserved.
* See LICENSE_MONSOON.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Monsoon\HyvaAjaxAddToCart\Model\ConfigInterface"
type="Monsoon\HyvaAjaxAddToCart\Model\Config" />
</config>
1 change: 1 addition & 0 deletions view/frontend/layout/hyva_default.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<body>
<referenceContainer name="before.body.end">
<block name="monsoon.ajax.addtocart"
class="Monsoon\HyvaAjaxAddToCart\Block\AddToCart"
template="Monsoon_HyvaAjaxAddToCart::hyva/script/addtocart.phtml"
ifconfig="checkout/options/enable_ajax_add_to_cart"/>
</referenceContainer>
Expand Down
9 changes: 6 additions & 3 deletions view/frontend/templates/hyva/script/addtocart.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/

Expand All @@ -24,6 +24,7 @@ use Magento\Framework\Escaper;
<script>
window.setAjaxCart = (selectors = '.product_addtocart_form, #product_addtocart_form', recursive = true) => {
const addToCartForms = document.querySelectorAll(selectors);
const delay = parseInt('<?= $escaper->escapeJs($block->getDelay()) ?>', 10);

addToCartForms.forEach(form => {
form.addEventListener('submit', e => {
Expand All @@ -46,6 +47,7 @@ use Magento\Framework\Escaper;

button.prepend(loader);
button.classList.add('relative', 'is-loading');
button.disabled = true;

fetch(e.currentTarget.action, {
method: 'POST',
Expand Down Expand Up @@ -95,11 +97,12 @@ use Magento\Framework\Escaper;
setTimeout(() => {
loader.remove();
button.classList.remove('is-loading');
button.disabled = false;

messages.forEach(message => {
setMessage(message);
});
}, 1000);
}, delay);
});
});
});
Expand Down

0 comments on commit fc27f6c

Please sign in to comment.