Skip to content

Commit

Permalink
add is_purchasable & can_show_price attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-kl1 committed Jan 4, 2022
1 parent 9463c72 commit b3fcabd
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 26 deletions.
15 changes: 7 additions & 8 deletions Observer/Product/IsSaleable.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,13 @@ public function execute(Observer $observer): void

if ($product instanceof DataObject && $saleable instanceof DataObject) {
$groupId = $this->httpContext->getValue(CustomerContext::CONTEXT_GROUP);
if ($groupId !== null) {
$saleable->setData(
'is_salable',
($saleable->getData('is_salable') && $product->getData('can_show_price'))
? $this->isSaleable->isSaleable((int) $groupId)
: false
);
}

$isSalable = $saleable->getData('is_salable')
&& $product->getData('can_show_price')
&& $product->getData('is_purchasable');
$isSalable = $isSalable && $groupId !== null ? $this->isSaleable->isSaleable((int) $groupId) : $isSalable;

$saleable->setData('is_salable', $isSalable);
}
}
}
16 changes: 11 additions & 5 deletions Plugin/Saleable/CanShowPrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Magento\Customer\Model\Context as CustomerContext;
use Magento\Framework\App\Http\Context as HttpContext;
use Opengento\Saleable\Api\CanShowPriceInterface;
use function var_dump;

final class CanShowPrice
{
Expand All @@ -29,13 +30,18 @@ public function __construct(
public function afterGetData(Product $product, $result, $key = null)
{
if ($key === 'can_show_price') {
$groupId = $this->httpContext->getValue(CustomerContext::CONTEXT_GROUP);
if ($groupId !== null) {
$result = ($result ?? true) ? $this->canShowPrice->canShowPrice((int) $groupId) : false;
}
$result = $result ? '1' : false;
$result = $this->canShowPrice((bool) $result);
} elseif ($key === null && isset($result['can_show_price'])) {
$result['can_show_price'] = $this->canShowPrice((bool) $result['can_show_price']);
}

return $result;
}

private function canShowPrice(bool $canShowPrice): bool
{
$groupId = $this->httpContext->getValue(CustomerContext::CONTEXT_GROUP);

return $canShowPrice && $groupId !== null ? $this->canShowPrice->canShowPrice((int) $groupId) : $canShowPrice;
}
}
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,16 @@ The configuration for this module is available in 'Stores > Configuration > Sale

- Enable Sales for Customer Groups

### Warning
### Product Attributes

If you need to determine the rules by products, create new product attributes:

- can_show_price (yes/no) The module has a plugin to enforce the type result to be a boolean.
- can_show_price (yes/no)
- is_purchasable (yes/no)

Do not create the following attributes as they are already in use internally by field alias and internal flag:
Do not create the following attributes as they are already used internally by Magento for field alias and flag:

- is_salable
- salable

Magento will automatically handle these attributes to check if a product is saleable or its price can be displayed.

## Support

Raise a new [request](https://github.com/opengento/magento2-saleable/issues) to the issue tracker.
Expand Down
99 changes: 99 additions & 0 deletions Setup/Patch/Data/ProductAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php
/**
* Copyright © OpenGento, All rights reserved.
* See LICENSE bundled with this library for license details.
*/
declare(strict_types=1);

namespace Opengento\Saleable\Setup\Patch\Data;

use Magento\Catalog\Model\Product;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Eav\Setup\EavSetup;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Zend_Validate_Exception;

final class ProductAttributes implements DataPatchInterface
{
private ModuleDataSetupInterface $moduleDataSetup;

private EavSetupFactory $eavSetupFactory;

public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
EavSetupFactory $eavSetupFactory
) {
$this->moduleDataSetup = $moduleDataSetup;
$this->eavSetupFactory = $eavSetupFactory;
}

/**
* @throws Zend_Validate_Exception
* @throws LocalizedException
*/
public function apply()
{
$this->moduleDataSetup->startSetup();

/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);

$eavSetup->addAttribute(Product::ENTITY, 'is_purchasable', [
'type' => 'int',
'backend' => '',
'frontend' => '',
'label' => 'Is Salable',
'input' => 'boolean',
'class' => '',
'source' => '',
'global' => ScopedAttributeInterface::SCOPE_STORE,
'visible' => true,
'required' => true,
'user_defined' => false,
'default' => true,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => true,
'unique' => false,
'apply_to' => '',
]);
$eavSetup->addAttribute(Product::ENTITY, 'can_show_price', [
'type' => 'int',
'backend' => '',
'frontend' => '',
'label' => 'Show Price',
'input' => 'boolean',
'class' => '',
'source' => '',
'global' => ScopedAttributeInterface::SCOPE_STORE,
'visible' => true,
'required' => true,
'user_defined' => false,
'default' => true,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => true,
'unique' => false,
'apply_to' => '',
]);

$this->moduleDataSetup->endSetup();
}

public function getAliases(): array
{
return [];
}

public static function getDependencies(): array
{
return [];
}
}
6 changes: 0 additions & 6 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,4 @@
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<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::isSaleable" type="Opengento\Saleable\Plugin\Pricing\CanShowPrice" sortOrder="10"/>
</type>
<type name="Magento\Catalog\Model\Product">
<plugin name="Opengento_Saleable::getCanShowPrice" type="Opengento\Saleable\Plugin\Saleable\CanShowPrice" sortOrder="10"/>
</type>
</config>
15 changes: 15 additions & 0 deletions etc/frontend/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © OpenGento, All rights reserved.
* See LICENSE bundled with this library for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\Pricing\SaleableInterface">
<plugin name="Opengento_Saleable::isSaleable" type="Opengento\Saleable\Plugin\Pricing\CanShowPrice" sortOrder="10"/>
</type>
<type name="Magento\Catalog\Model\Product">
<plugin name="Opengento_Saleable::getCanShowPrice" type="Opengento\Saleable\Plugin\Saleable\CanShowPrice" sortOrder="10"/>
</type>
</config>
File renamed without changes.

0 comments on commit b3fcabd

Please sign in to comment.