Skip to content

Commit

Permalink
v1
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-kl1 committed May 10, 2020
0 parents commit f5e51a2
Show file tree
Hide file tree
Showing 18 changed files with 911 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Api/CanShowPriceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* Copyright © OpenGento, All rights reserved.
* See LICENSE bundled with this library for license details.
*/
declare(strict_types=1);

namespace Opengento\Saleable\Api;

/**
* @api
*/
interface CanShowPriceInterface
{
public function canShowPrice(int $customerGroupId): bool;
}
16 changes: 16 additions & 0 deletions Api/IsSaleableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* Copyright © OpenGento, All rights reserved.
* See LICENSE bundled with this library for license details.
*/
declare(strict_types=1);

namespace Opengento\Saleable\Api;

/**
* @api
*/
interface IsSaleableInterface
{
public function isSaleable(int $customerGroupId): bool;
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) OpenGento

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
67 changes: 67 additions & 0 deletions Model/CanShowPrice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Copyright © OpenGento, All rights reserved.
* See LICENSE bundled with this library for license details.
*/
declare(strict_types=1);

namespace Opengento\Saleable\Model;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
use Opengento\Saleable\Api\CanShowPriceInterface;
use function array_filter;
use function array_map;
use function explode;
use function in_array;

final class CanShowPrice implements CanShowPriceInterface
{
private const CONFIG_PATH_RESTRICT_SHOW_PRICE = 'catalog/price/restrict_show_price';
private const CONFIG_PATH_CAN_SHOW_PRICE_GROUPS = 'catalog/price/can_show_price_groups';

/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* @var array|null
*/
private $allowedGroups;

/**
* @var bool|null
*/
private $isEnabled;

public function __construct(
ScopeConfigInterface $scopeConfig
) {
$this->scopeConfig = $scopeConfig;
}

public function canShowPrice(int $customerGroupId): bool
{
return $this->isEnabled() ? in_array($customerGroupId, $this->resolveAllowedGroups(), true) : true;
}

private function isEnabled(): bool
{
return $this->isEnabled ?? $this->isEnabled = $this->scopeConfig->isSetFlag(
self::CONFIG_PATH_RESTRICT_SHOW_PRICE,
ScopeInterface::SCOPE_WEBSITE
);
}

private function resolveAllowedGroups(): array
{
return $this->allowedGroups
?? $this->allowedGroups = array_map('\intval', array_filter(
explode(',', (string) $this->scopeConfig->getValue(
self::CONFIG_PATH_CAN_SHOW_PRICE_GROUPS,
ScopeInterface::SCOPE_WEBSITE
))
));
}
}
76 changes: 76 additions & 0 deletions Model/IsSaleable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Copyright © OpenGento, All rights reserved.
* See LICENSE bundled with this library for license details.
*/
declare(strict_types=1);

namespace Opengento\Saleable\Model;

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;
use function explode;
use function in_array;

final class IsSaleable implements IsSaleableInterface
{
private const CONFIG_PATH_RESTRICT_SALEABLE = 'checkout/cart/restrict_saleable';
private const CONFIG_PATH_IS_SALEABLE_GROUPS = 'checkout/cart/is_saleable_groups';

/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

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

/**
* @var array|null
*/
private $allowedGroups;

/**
* @var bool|null
*/
private $isEnabled;

public function __construct(
ScopeConfigInterface $scopeConfig,
CanShowPriceInterface $canShowPrice
) {
$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));
}

private function isEnabled(): bool
{
return $this->isEnabled ?? $this->isEnabled = $this->scopeConfig->isSetFlag(
self::CONFIG_PATH_RESTRICT_SALEABLE,
ScopeInterface::SCOPE_WEBSITE
);
}

private function resolveAllowedGroups(): array
{
return $this->allowedGroups
?? $this->allowedGroups = array_map('\intval', array_filter(
explode(',', (string) $this->scopeConfig->getValue(
self::CONFIG_PATH_IS_SALEABLE_GROUPS,
ScopeInterface::SCOPE_WEBSITE
))
));
}
}
50 changes: 50 additions & 0 deletions Observer/Product/IsSaleable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Copyright © OpenGento, All rights reserved.
* See LICENSE bundled with this library for license details.
*/
declare(strict_types=1);

namespace Opengento\Saleable\Observer\Product;

use Magento\Customer\Model\Context as CustomerContext;
use Magento\Framework\App\Http\Context as HttpContext;
use Magento\Framework\DataObject;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Opengento\Saleable\Api\IsSaleableInterface;

final class IsSaleable implements ObserverInterface
{
/**
* @var HttpContext
*/
private $httpContext;

/**
* @var IsSaleableInterface
*/
private $isSaleable;

public function __construct(
HttpContext $httpContext,
IsSaleableInterface $isSaleable
) {
$this->httpContext = $httpContext;
$this->isSaleable = $isSaleable;
}

public function execute(Observer $observer): void
{
$saleable = $observer->getData('salable');

if ($saleable instanceof DataObject) {
$saleable->setData(
'is_salable',
(bool) $saleable->getData('is_salable')
? $this->isSaleable->isSaleable((int) $this->httpContext->getValue(CustomerContext::CONTEXT_GROUP))
: false
);
}
}
}
41 changes: 41 additions & 0 deletions Plugin/Pricing/CanShowPrice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* Copyright © OpenGento, All rights reserved.
* See LICENSE bundled with this library for license details.
*/
declare(strict_types=1);

namespace Opengento\Saleable\Plugin\Pricing;

use Magento\Customer\Model\Context as CustomerContext;
use Magento\Framework\App\Http\Context as HttpContext;
use Magento\Framework\Pricing\SaleableInterface;
use Opengento\Saleable\Api\CanShowPriceInterface;

final class CanShowPrice
{
/**
* @var HttpContext
*/
private $httpContext;

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

public function __construct(
HttpContext $httpContext,
CanShowPriceInterface $canShowPrice
) {
$this->httpContext = $httpContext;
$this->canShowPrice = $canShowPrice;
}

public function afterGetCanShowPrice(SaleableInterface $saleable, bool $canShowPrice): bool
{
return $canShowPrice
? $this->canShowPrice->canShowPrice((int) $this->httpContext->getValue(CustomerContext::CONTEXT_GROUP))
: false;
}
}
41 changes: 41 additions & 0 deletions Plugin/Pricing/Renderer/CanShowPrice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* Copyright © OpenGento, All rights reserved.
* See LICENSE bundled with this library for license details.
*/
declare(strict_types=1);

namespace Opengento\Saleable\Plugin\Pricing\Renderer;

use Magento\Catalog\Model\Product\Pricing\Renderer\SalableResolverInterface;
use Magento\Customer\Model\Context as CustomerContext;
use Magento\Framework\App\Http\Context as HttpContext;
use Opengento\Saleable\Api\CanShowPriceInterface;

final class CanShowPrice
{
/**
* @var HttpContext
*/
private $httpContext;

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

public function __construct(
HttpContext $httpContext,
CanShowPriceInterface $canShowPrice
) {
$this->httpContext = $httpContext;
$this->canShowPrice = $canShowPrice;
}

public function afterIsSalable(SalableResolverInterface $salableResolver, bool $isSalable): bool
{
return $isSalable
? $this->canShowPrice->canShowPrice((int) $this->httpContext->getValue(CustomerContext::CONTEXT_GROUP))
: false;
}
}
Loading

0 comments on commit f5e51a2

Please sign in to comment.