forked from Smile-SA/elasticsuite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature Smile-SA#3038 Use display_pattern on product view, product co…
…mpare pages (disabled by default)
- Loading branch information
Showing
6 changed files
with
300 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
src/module-elasticsuite-catalog/Plugin/Catalog/Product/Compare/ListComparePlugin.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?php | ||
/** | ||
* DISCLAIMER | ||
* | ||
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer | ||
* versions in the future. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteCatalog | ||
* @author Edward Crocombe <[email protected]> | ||
* @copyright 2020 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
namespace Smile\ElasticsuiteCatalog\Plugin\Catalog\Product\View; | ||
|
||
use Magento\Catalog\Api\ProductAttributeRepositoryInterface; | ||
use Smile\ElasticsuiteCatalog\Helper\ProductAttribute; | ||
|
||
/** | ||
* Catalog Product List Compare plugin. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteCatalog | ||
* @author Edward Crocombe <[email protected]> | ||
*/ | ||
class ListComparePlugin | ||
{ | ||
/** | ||
* @var null|\Magento\Catalog\Api\Data\ProductAttributeInterface[] | ||
*/ | ||
private $attributes; | ||
|
||
/** | ||
* @var \Smile\ElasticsuiteCatalog\Helper\ProductAttribute | ||
*/ | ||
private $productAttributeHelper; | ||
|
||
/** | ||
* @var \Magento\Catalog\Api\ProductAttributeRepositoryInterface $productAttributeRepository | ||
*/ | ||
private $productAttributeRepository; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param ProductAttribute $productAttributeHelper ElasticSuite product attributes helper. | ||
* @param ProductAttributeRepositoryInterface $productAttributeRepository Formats numbers to a locale | ||
*/ | ||
public function __construct( | ||
ProductAttribute $productAttributeHelper, | ||
ProductAttributeRepositoryInterface $productAttributeRepository | ||
) { | ||
$this->productAttributeHelper = $productAttributeHelper; | ||
$this->productAttributeRepository = $productAttributeRepository; | ||
} | ||
|
||
/** | ||
* Add display pattern for frontend display | ||
* | ||
* @param \Magento\Catalog\Block\Product\Compare\ListCompare $subject Plugin Subject | ||
* @param \Magento\Framework\Phrase|string $result Plugin Result | ||
* @param \Magento\Catalog\Model\Product $product Product | ||
* @param \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute Product Attribute | ||
* | ||
* @return \Magento\Framework\Phrase|string | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function afterGetProductAttributeValue( | ||
\Magento\Catalog\Block\Product\Compare\ListCompare $subject, | ||
$result, | ||
$product, | ||
$attribute | ||
) { | ||
if (!$this->productAttributeHelper->isFrontendProductDisplayPatternEnabled()) { | ||
return $result; | ||
} | ||
|
||
$value = $attribute->getFrontend()->getValue($product); | ||
if (is_numeric($value) && strlen($attribute->getData('display_pattern') ?? '') > 0) { | ||
$result = $this->productAttributeHelper->formatProductAttributeValueDisplayPattern($attribute, $value); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* Retrieve Product Compare Attributes | ||
* | ||
* Default getAttributes retrieves columns from eav_attribute table only, | ||
* both the display_pattern and display_precision values are on the catalog_eav_attribute table. | ||
* | ||
* @param \Magento\Catalog\Block\Product\Compare\ListCompare $subject Plugin Subject | ||
* @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute[] $result Plugin Result | ||
* | ||
* @return \Magento\Catalog\Api\Data\ProductAttributeInterface[] | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function afterGetAttributes(\Magento\Catalog\Block\Product\Compare\ListCompare $subject, $result) | ||
{ | ||
if (!$this->productAttributeHelper->isFrontendProductDisplayPatternEnabled()) { | ||
return $result; | ||
} | ||
|
||
if ($this->attributes === null) { | ||
$this->attributes = []; | ||
foreach (array_keys($result) as $attributeCode) { | ||
$this->attributes[$attributeCode] = $this->productAttributeRepository->get($attributeCode); | ||
} | ||
} | ||
|
||
return $this->attributes; | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
src/module-elasticsuite-catalog/Plugin/Catalog/Product/View/AttributesPlugin.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
/** | ||
* DISCLAIMER | ||
* | ||
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer | ||
* versions in the future. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteCatalog | ||
* @author Edward Crocombe <[email protected]> | ||
* @copyright 2020 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
namespace Smile\ElasticsuiteCatalog\Plugin\Catalog\Product\View; | ||
|
||
use Smile\ElasticsuiteCatalog\Helper\ProductAttribute; | ||
|
||
/** | ||
* Catalog Product View Attributes plugin. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteCatalog | ||
* @author Edward Crocombe <[email protected]> | ||
*/ | ||
class AttributesPlugin | ||
{ | ||
/** | ||
* @var \Smile\ElasticsuiteCatalog\Helper\ProductAttribute | ||
*/ | ||
private $productAttributeHelper; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param ProductAttribute $productAttributeHelper ElasticSuite product attributes helper. | ||
*/ | ||
public function __construct( | ||
ProductAttribute $productAttributeHelper | ||
) { | ||
$this->productAttributeHelper = $productAttributeHelper; | ||
} | ||
|
||
/** | ||
* Add display pattern for frontend display | ||
* | ||
* @param \Magento\Catalog\Block\Product\View\Attributes $subject Plugin Subject | ||
* @param array $result Additional data | ||
* @param string[] $excludeAttr Attribute Codes to exclude | ||
* | ||
* @return array Additional data | ||
* @throws \Magento\Framework\Exception\LocalizedException | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function afterGetAdditionalData(\Magento\Catalog\Block\Product\View\Attributes $subject, $result, array $excludeAttr = []) | ||
{ | ||
if (!$this->productAttributeHelper->isFrontendProductDisplayPatternEnabled()) { | ||
return $result; | ||
} | ||
|
||
$product = $subject->getProduct(); | ||
$attributes = $product->getAttributes(); | ||
foreach ($attributes as $attribute) { | ||
// If attribute is already in array, then isVisibleOnFrontend = `true`. | ||
if (isset($result[$attribute->getAttributeCode()])) { | ||
// @codingStandardsIgnoreStart | ||
$value = isset($result[$attribute->getAttributeCode()]['value']) | ||
? $result[$attribute->getAttributeCode()]['value'] | ||
: ''; | ||
// @codingStandardsIgnoreEnd | ||
|
||
if (is_numeric($value) && strlen($attribute->getData('display_pattern') ?? '') > 0) { | ||
$result[$attribute->getAttributeCode()]['value'] | ||
= $this->productAttributeHelper->formatProductAttributeValueDisplayPattern($attribute, $value); | ||
} | ||
} | ||
} | ||
|
||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters