Skip to content

Commit

Permalink
Merge pull request #16 from sequra/fix/widget-amount
Browse files Browse the repository at this point in the history
Fix widget amount
  • Loading branch information
m1k3lm authored Apr 8, 2024
2 parents aea429a + b4a7849 commit 42defff
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 28 deletions.
132 changes: 106 additions & 26 deletions Block/WidgetInitializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
namespace Sequra\Core\Block;

use Exception;
use Magento\Catalog\Helper\Data;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ProductRepository;
use Magento\Checkout\Block\Cart;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Request\Http;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Quote\Model\Quote\Item;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;
use SeQura\Core\BusinessLogic\Domain\GeneralSettings\Models\GeneralSettings;
use SeQura\Core\BusinessLogic\Domain\GeneralSettings\Services\GeneralSettingsService;
use SeQura\Core\BusinessLogic\Domain\Multistore\StoreContext;
Expand Down Expand Up @@ -52,6 +56,18 @@ class WidgetInitializer extends Template
* @var PriceCurrencyInterface
*/
private $priceCurrency;
/**
* @var ScopeConfigInterface
*/
private $scopeConfig;
/**
* @var StoreManagerInterface
*/
private $storeManager;
/**
* @var Data
*/
private $catalogHelper;

/**
* @param WidgetConfigService $widgetConfigService
Expand All @@ -60,18 +76,24 @@ class WidgetInitializer extends Template
* @param Cart $cart
* @param ProductService $productService
* @param PriceCurrencyInterface $priceCurrency
* @param ScopeConfigInterface $scopeConfig
* @param StoreManagerInterface $storeManager
* @param Data $catalogHelper
* @param Context $context
* @param array $data
*/
public function __construct(
WidgetConfigService $widgetConfigService,
Http $request,
ProductRepository $productRepository,
Cart $cart,
ProductService $productService,
PriceCurrencyInterface $priceCurrency,
Template\Context $context,
array $data = []
WidgetConfigService $widgetConfigService,
Http $request,
ProductRepository $productRepository,
Cart $cart,
ProductService $productService,
PriceCurrencyInterface $priceCurrency,
ScopeConfigInterface $scopeConfig,
StoreManagerInterface $storeManager,
Data $catalogHelper,
Template\Context $context,
array $data = []
)
{
parent::__construct($context, $data);
Expand All @@ -82,6 +104,9 @@ public function __construct(
$this->cart = $cart;
$this->productService = $productService;
$this->priceCurrency = $priceCurrency;
$this->scopeConfig = $scopeConfig;
$this->storeManager = $storeManager;
$this->catalogHelper = $catalogHelper;
}

/**
Expand Down Expand Up @@ -128,20 +153,8 @@ private function getConfig(string $actionName): array
$widgetSettings = $this->getWidgetSettings();
$settings = $this->getGeneralSettings();

if (empty($widgetSettings) || !$widgetSettings->isEnabled()) {
return [];
}

if ($this->priceCurrency->getCurrency()->getCurrencyCode() !== 'EUR' ||
($settings && !empty($settings->getAllowedIPAddresses()) && !empty($ipAddress = $this->getCustomerIpAddress()) &&
!in_array($ipAddress, $settings->getAllowedIPAddresses(), true))
||
($actionName === 'catalog_product_view' && !$widgetSettings->isDisplayOnProductPage()
&& !$widgetSettings->isShowInstallmentsInProductListing()) ||
($actionName === 'checkout_cart_index' && !$widgetSettings->isShowInstallmentsInCartPage())
|| ($actionName === 'catalog_category_view' && !$widgetSettings->isShowInstallmentsInProductListing())
|| (($actionName === 'cms_index_index' || $actionName === 'catalogsearch_result_index')
&& !$widgetSettings->isShowInstallmentsInProductListing())) {
if (empty($widgetSettings) || !$widgetSettings->isEnabled() ||
$this->shouldNotDisplayWidgets($settings, $widgetSettings, $actionName)) {
return [];
}

Expand All @@ -153,9 +166,7 @@ private function getConfig(string $actionName): array
return [];
}

$amount = ($product->getTypeId() === 'bundle' ?
$product->getPriceInfo()->getPrice('regular_price')->getMinimalPrice()->getValue() :
$product->getFinalPrice()) * 100;
$amount = $this->getProductPrice($product);
}

if ($actionName === 'checkout_cart_index') {
Expand All @@ -174,7 +185,31 @@ private function getConfig(string $actionName): array

$config = $this->widgetConfigService->getData($this->_storeManager->getStore()->getId());

return $config ? array_merge($config, ['amount' => $amount, 'action_name' => $actionName]) : [];
return $config ? array_merge($config, ['amount' => (int)round($amount), 'action_name' => $actionName]) : [];
}

/**
* @param GeneralSettings|null $settings
* @param WidgetSettings|null $widgetSettings
* @param string $actionName
*
* @return bool
*/
private function shouldNotDisplayWidgets(
?GeneralSettings $settings,
?WidgetSettings $widgetSettings,
string $actionName
): bool
{
return $this->priceCurrency->getCurrency()->getCurrencyCode() !== 'EUR' ||
($settings && !empty($settings->getAllowedIPAddresses()) && !empty($ipAddress = $this->getCustomerIpAddress()) &&
!in_array($ipAddress, $settings->getAllowedIPAddresses(), true))
|| ($actionName === 'catalog_product_view' && !$widgetSettings->isDisplayOnProductPage()
&& !$widgetSettings->isShowInstallmentsInProductListing()) ||
($actionName === 'checkout_cart_index' && !$widgetSettings->isShowInstallmentsInCartPage())
|| ($actionName === 'catalog_category_view' && !$widgetSettings->isShowInstallmentsInProductListing())
|| (($actionName === 'cms_index_index' || $actionName === 'catalogsearch_result_index')
&& !$widgetSettings->isShowInstallmentsInProductListing());
}

/**
Expand Down Expand Up @@ -219,6 +254,51 @@ private function getWidgetSettings(): ?WidgetSettings
return $widgetService->getWidgetSettings();
}

/**
* @param Product $product
* @return float|int
*
* @throws NoSuchEntityException
*/
private function getProductPrice(Product $product)
{
$price = ($product->getTypeId() === 'bundle' ?
$product->getPriceInfo()->getPrice('regular_price')->getMinimalPrice()->getValue() :
$product->getFinalPrice());

if ($product->getTypeId() === 'bundle' || !$this->isTaxEnabled()) {
return $price * 100;
}

return $this->catalogHelper->getTaxPrice($product, $price, true) * 100;
}

/**
* @return bool
*
* @throws NoSuchEntityException
*/
private function isTaxEnabled(): bool
{
$storeId = StoreContext::getInstance()->getStoreId();
$taxSettings = $this->scopeConfig->getValue('tax/display/type', ScopeInterface::SCOPE_STORES, $storeId);

if ($taxSettings) {
return $taxSettings > 1;
}

$store = $this->storeManager->getStore($storeId);
$taxSettings = $this->scopeConfig->getValue('tax/display/type', ScopeInterface::SCOPE_WEBSITES, $store->getWebsiteId());

if ($taxSettings) {
return $taxSettings > 1;
}

$taxSettings = $this->scopeConfig->getValue('tax/display/type');

return $taxSettings > 1;
}

private function getCustomerIpAddress(): string
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
Expand Down
2 changes: 1 addition & 1 deletion Plugin/MiniWidgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function afterToHtml(Amount $subject, $result): string
$store = $this->storeManager->getStore();
$product = $subject->getPrice()->getProduct();

$amount = (int)round($subject->getPrice()->getValue() * 100);
$amount = (int)round($subject->getPrice()->getAmount()->getValue() * 100);
$result .= StoreContext::doWithStore($store->getId(), function () use ($amount, $store, $product) {
return $this->getHtml($amount, $store, $product);
});
Expand Down
2 changes: 1 addition & 1 deletion view/frontend/templates/scripts.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
let sequraElements = document.getElementsByClassName('sequra-promotion-widget');

[...sequraElements].forEach((el) => {
el.setAttribute('data-amount', (Number(grandTotal.innerText.replace(/[^0-9\.-]+/g, "")) * 100));
el.setAttribute('data-amount', Math.round(Number(grandTotal.innerText.replace(/[^0-9\.-]+/g, "")) * 100));
});

Sequra.refreshComponents?.();
Expand Down

0 comments on commit 42defff

Please sign in to comment.