diff --git a/Model/Config/Backend/Cache.php b/Model/Config/Backend/Cache.php new file mode 100644 index 0000000..96bac96 --- /dev/null +++ b/Model/Config/Backend/Cache.php @@ -0,0 +1,32 @@ +isValueChanged()) { + $this->cacheTypeList->invalidate(Block::TYPE_IDENTIFIER); + } + + return parent::afterSave(); + } + + public function afterDelete(): Cache + { + if ($this->isValueChanged()) { + $this->cacheTypeList->invalidate(Block::TYPE_IDENTIFIER); + } + + return parent::afterDelete(); + } +} diff --git a/Model/IsSaleable.php b/Model/IsSaleable.php index bb30e94..c05099d 100644 --- a/Model/IsSaleable.php +++ b/Model/IsSaleable.php @@ -9,7 +9,6 @@ use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Store\Model\ScopeInterface; -use Opengento\Saleable\Api\CanShowPriceInterface; use Opengento\Saleable\Api\IsSaleableInterface; use function array_filter; use function array_map; @@ -26,11 +25,6 @@ final class IsSaleable implements IsSaleableInterface */ private $scopeConfig; - /** - * @var CanShowPriceInterface - */ - private $canShowPrice; - /** * @var array|null */ @@ -42,17 +36,14 @@ final class IsSaleable implements IsSaleableInterface private $isEnabled; public function __construct( - ScopeConfigInterface $scopeConfig, - CanShowPriceInterface $canShowPrice + ScopeConfigInterface $scopeConfig ) { $this->scopeConfig = $scopeConfig; - $this->canShowPrice = $canShowPrice; } public function isSaleable(int $customerGroupId): bool { - return $this->canShowPrice->canShowPrice($customerGroupId) - && (!$this->isEnabled() || in_array($customerGroupId, $this->resolveAllowedGroups(), true)); + return (!$this->isEnabled() || in_array($customerGroupId, $this->resolveAllowedGroups(), true)); } private function isEnabled(): bool diff --git a/Observer/Product/IsSaleable.php b/Observer/Product/IsSaleable.php index 134a9c2..6033af9 100644 --- a/Observer/Product/IsSaleable.php +++ b/Observer/Product/IsSaleable.php @@ -36,12 +36,13 @@ public function __construct( public function execute(Observer $observer): void { + $product = $observer->getData('product'); $saleable = $observer->getData('salable'); - if ($saleable instanceof DataObject) { + if ($product instanceof DataObject && $saleable instanceof DataObject) { $saleable->setData( 'is_salable', - (bool) $saleable->getData('is_salable') + ($saleable->getData('is_salable') && $product->getData('can_show_price')) ? $this->isSaleable->isSaleable((int) $this->httpContext->getValue(CustomerContext::CONTEXT_GROUP)) : false ); diff --git a/Plugin/Pricing/Renderer/CanShowPrice.php b/Plugin/Saleable/CanShowPrice.php similarity index 63% rename from Plugin/Pricing/Renderer/CanShowPrice.php rename to Plugin/Saleable/CanShowPrice.php index 48cd368..50101a3 100644 --- a/Plugin/Pricing/Renderer/CanShowPrice.php +++ b/Plugin/Saleable/CanShowPrice.php @@ -5,9 +5,9 @@ */ declare(strict_types=1); -namespace Opengento\Saleable\Plugin\Pricing\Renderer; +namespace Opengento\Saleable\Plugin\Saleable; -use Magento\Catalog\Model\Product\Pricing\Renderer\SalableResolverInterface; +use Magento\Catalog\Model\Product; use Magento\Customer\Model\Context as CustomerContext; use Magento\Framework\App\Http\Context as HttpContext; use Opengento\Saleable\Api\CanShowPriceInterface; @@ -32,10 +32,14 @@ public function __construct( $this->canShowPrice = $canShowPrice; } - public function afterIsSalable(SalableResolverInterface $salableResolver, bool $isSalable): bool + public function afterGetData(Product $product, $result, $key = '') { - return $isSalable - ? $this->canShowPrice->canShowPrice((int) $this->httpContext->getValue(CustomerContext::CONTEXT_GROUP)) - : false; + if ($key === 'can_show_price') { + return (bool) ($result ?? true) + ? $this->canShowPrice->canShowPrice((int) $this->httpContext->getValue(CustomerContext::CONTEXT_GROUP)) + : false; + } + + return $result; } } diff --git a/README.md b/README.md index 967a4bd..a0d82a7 100644 --- a/README.md +++ b/README.md @@ -57,10 +57,10 @@ The configuration for this module is available in 'Stores > Configuration > Sale ### Warning -If you need to determine the rules by products, do not use this module, instead create new product attributes: +If you need to determine the rules by products, create new product attributes: -- can_show_price (boolean) -- salable (boolean) +- can_show_price (yes/no) The module has a plugin to enforce the type result to be a boolean. +- salable (yes/no) The module has a plugin to enforce the type result to be a boolean. Magento will automatically handle these attributes to check if a product is saleable or its price can be displayed. diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index a481c3d..3058b14 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -12,12 +12,14 @@ Magento\Config\Model\Config\Source\Enabledisable + Opengento\Saleable\Model\Config\Backend\Cache catalog/price/restrict_show_price Not logged in users will never been able to see prices. Magento\Customer\Model\Config\Source\Group\Multiselect + Opengento\Saleable\Model\Config\Backend\Cache 1 1 diff --git a/etc/di.xml b/etc/di.xml index 1334ad6..4912c72 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -9,9 +9,9 @@ - + - - + + diff --git a/etc/events.xml b/etc/events.xml index a4fc4bb..bfc5523 100644 --- a/etc/events.xml +++ b/etc/events.xml @@ -7,6 +7,6 @@ --> - +