Skip to content

Commit

Permalink
fix type when attributes exists
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-kl1 committed Jun 8, 2020
1 parent f5e51a2 commit eec6731
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 26 deletions.
32 changes: 32 additions & 0 deletions Model/Config/Backend/Cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Copyright © OpenGento, All rights reserved.
* See LICENSE bundled with this library for license details.
*/
declare(strict_types=1);

namespace Opengento\Saleable\Model\Config\Backend;

use Magento\Framework\App\Cache\Type\Block;
use Magento\Framework\App\Config\Value;

class Cache extends Value
{
public function afterSave(): Cache
{
if ($this->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();
}
}
13 changes: 2 additions & 11 deletions Model/IsSaleable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,11 +25,6 @@ final class IsSaleable implements IsSaleableInterface
*/
private $scopeConfig;

/**
* @var CanShowPriceInterface
*/
private $canShowPrice;

/**
* @var array|null
*/
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions Observer/Product/IsSaleable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 2 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
<field id="restrict_show_price" translate="label" type="select" showInDefault="1" showInWebsite="1" showInStore="0" sortOrder="10" canRestore="1">
<label>Restrict Show Prices Mode</label>
<source_model>Magento\Config\Model\Config\Source\Enabledisable</source_model>
<backend_model>Opengento\Saleable\Model\Config\Backend\Cache</backend_model>
<config_path>catalog/price/restrict_show_price</config_path>
</field>
<field id="can_show_price_groups" translate="label comment" type="multiselect" showInDefault="1" showInWebsite="1" showInStore="0" sortOrder="20">
<label>Show Prices for Customer Groups</label>
<comment>Not logged in users will never been able to see prices.</comment>
<source_model>Magento\Customer\Model\Config\Source\Group\Multiselect</source_model>
<backend_model>Opengento\Saleable\Model\Config\Backend\Cache</backend_model>
<can_be_empty>1</can_be_empty>
<depends>
<field id="restrict_show_price">1</field>
Expand Down
6 changes: 3 additions & 3 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
<preference for="Opengento\Saleable\Api\CanShowPriceInterface" type="Opengento\Saleable\Model\CanShowPrice"/>
<preference for="Opengento\Saleable\Api\IsSaleableInterface" type="Opengento\Saleable\Model\IsSaleable"/>
<type name="Magento\Framework\Pricing\SaleableInterface">
<plugin name="Opengento_Saleable::canShowPrice" type="Opengento\Saleable\Plugin\Pricing\CanShowPrice" sortOrder="10"/>
<plugin name="Opengento_Saleable::isSaleable" type="Opengento\Saleable\Plugin\Pricing\CanShowPrice" sortOrder="10"/>
</type>
<type name="Magento\Catalog\Model\Product\Pricing\Renderer\SalableResolverInterface">
<plugin name="Opengento_Saleable::isSaleable" type="Opengento\Saleable\Plugin\Pricing\Renderer\CanShowPrice" sortOrder="10"/>
<type name="Magento\Catalog\Model\Product">
<plugin name="Opengento_Saleable::getCanShowPrice" type="Opengento\Saleable\Plugin\Saleable\CanShowPrice" sortOrder="10"/>
</type>
</config>
2 changes: 1 addition & 1 deletion etc/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="catalog_product_is_salable_after">
<observer name="Opengento_Saleable::canShowPrice" instance="Opengento\Saleable\Observer\Product\IsSaleable" />
<observer name="Opengento_Saleable::isSaleable" instance="Opengento\Saleable\Observer\Product\IsSaleable" />
</event>
</config>

0 comments on commit eec6731

Please sign in to comment.