-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2304 from PierreGauthier/fix-esp-286-visutal-cate…
…gory-inpagebuilder [Virtual Category] Pagebuilder Compatibility #ESP-286
- Loading branch information
Showing
9 changed files
with
467 additions
and
16 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
44 changes: 44 additions & 0 deletions
44
src/module-elasticsuite-virtual-category/Model/Condition/ElasticsearchBuilder.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,44 @@ | ||
<?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\ElasticsuiteVirtualCategory | ||
* @author Pierre Gauthier <pigau@smile.fr> | ||
* @copyright 2021 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
|
||
namespace Smile\ElasticsuiteVirtualCategory\Model\Condition; | ||
|
||
use Magento\Eav\Model\Entity\Collection\AbstractCollection; | ||
use Magento\Rule\Model\Condition\Combine; | ||
use Magento\Rule\Model\Condition\Sql\Builder; | ||
|
||
/** | ||
* Disable SQL Builder and create elasticsearch query from condition. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteVirtualCategory | ||
* @author Pierre Gauthier <pigau@smile.fr> | ||
*/ | ||
class ElasticsearchBuilder extends Builder | ||
{ | ||
/** | ||
* Attach conditions filter to collection | ||
* | ||
* @param AbstractCollection $collection Product Collection. | ||
* @param Combine $combine Conditions. | ||
* @return void | ||
*/ | ||
public function attachConditionToCollection( | ||
AbstractCollection $collection, | ||
Combine $combine | ||
): void { | ||
$query = $combine->getSearchQuery(); | ||
$collection->addQueryFilter($query); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/module-elasticsuite-virtual-category/Model/Rule/WidgetCondition/Combine.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,51 @@ | ||
<?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\ElasticsuiteVirtualCategory | ||
* @author Pierre Gauthier <pigau@smile.fr> | ||
* @copyright 2020 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
|
||
namespace Smile\ElasticsuiteVirtualCategory\Model\Rule\WidgetCondition; | ||
|
||
use Magento\Catalog\Model\ResourceModel\Product\Collection; | ||
|
||
/** | ||
* Combine product search rule conditions for pagebuilder widget. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteVirtualCategory | ||
* @author Pierre Gauthier <pigau@smile.fr> | ||
*/ | ||
class Combine extends \Smile\ElasticsuiteVirtualCategory\Model\Rule\Condition\Combine | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $elementName = 'parameters'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $type = 'Smile\ElasticsuiteVirtualCategory\Model\Rule\WidgetCondition\Combine'; | ||
|
||
/** | ||
* Collect validated attributes | ||
* | ||
* @param Collection $collection Product collection. | ||
* @return self | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function collectValidatedAttributes(Collection $collection) | ||
{ | ||
// Create this empty function in order to avoid undefined error | ||
// @see magento/module-catalog-widget/Block/Product/ProductsList.php:351. | ||
return $this; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/module-elasticsuite-virtual-category/Model/Rule/WidgetCondition/Product.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,43 @@ | ||
<?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\ElasticsuiteVirtualCategory | ||
* @author Pierre Gauthier <pigau@smile.fr> | ||
* @copyright 2021 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
|
||
namespace Smile\ElasticsuiteVirtualCategory\Model\Rule\WidgetCondition; | ||
|
||
use Smile\ElasticsuiteCore\Search\Request\QueryInterface; | ||
|
||
/** | ||
* Product search rule condition for pagebuilder widget. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteVirtualCategory | ||
* @author Pierre Gauthier <pigau@smile.fr> | ||
*/ | ||
class Product extends \Smile\ElasticsuiteVirtualCategory\Model\Rule\Condition\Product | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $elementName = 'parameters'; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getSearchQuery($excludedCategories = [], $virtualCategoryRoot = null): ?QueryInterface | ||
{ | ||
// Fix some js encoding error with operators. | ||
$this->setData('operator', htmlspecialchars_decode($this->getOperator())); | ||
|
||
return parent::getSearchQuery($excludedCategories, $virtualCategoryRoot); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
src/module-elasticsuite-virtual-category/Plugin/Collection/AddVirtualCategoryQuery.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,65 @@ | ||
<?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\ElasticsuiteVirtualCategory | ||
* @author Pierre Gauthier <pigau@smile.fr> | ||
* @copyright 2021 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
|
||
namespace Smile\ElasticsuiteVirtualCategory\Plugin\Collection; | ||
|
||
use Magento\Catalog\Model\Category; | ||
use Smile\ElasticsuiteCatalog\Model\ResourceModel\Product\Fulltext\Collection; | ||
use Smile\ElasticsuiteVirtualCategory\Model\Category\Filter\Provider; | ||
use Smile\ElasticsuiteVirtualCategory\Model\ResourceModel\Product\CollectionFactory; | ||
|
||
/** | ||
* Add virtual category query in product collection filters. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteVirtualCategory | ||
* @author Pierre Gauthier <pigau@smile.fr> | ||
*/ | ||
class AddVirtualCategoryQuery | ||
{ | ||
/** | ||
* @var Provider | ||
*/ | ||
private $filterProvider; | ||
|
||
/** | ||
* AddVirtualCategoryQuery constructor. | ||
* | ||
* @param Provider $filterProvider Filter prodivder. | ||
*/ | ||
public function __construct(Provider $filterProvider) | ||
{ | ||
$this->filterProvider = $filterProvider; | ||
} | ||
|
||
/** | ||
* Add virtual category query in collection filters. | ||
* | ||
* @param Collection $subject Product collection. | ||
* @param Category $category Category. | ||
* @return array | ||
*/ | ||
public function beforeAddCategoryFilter(Collection $subject, Category $category) | ||
{ | ||
if ($category && $category->getData('is_virtual_category')) { | ||
$query = $this->filterProvider->getQueryFilter($category); | ||
if ($query !== null) { | ||
$subject->addQueryFilter($query); | ||
} | ||
$category->setId(null); | ||
} | ||
|
||
return [$category]; | ||
} | ||
} |
117 changes: 117 additions & 0 deletions
117
src/module-elasticsuite-virtual-category/Plugin/Widget/ProductsListPlugin.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,117 @@ | ||
<?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\ElasticsuiteVirtualCategory | ||
* @author Pierre Gauthier <pigau@smile.fr> | ||
* @copyright 2021 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
|
||
namespace Smile\ElasticsuiteVirtualCategory\Plugin\Widget; | ||
|
||
use Magento\Catalog\Api\CategoryRepositoryInterface; | ||
use Magento\CatalogWidget\Block\Product\ProductsList; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\Store\Model\StoreManagerInterface; | ||
use Magento\Widget\Helper\Conditions; | ||
use Smile\ElasticsuiteCatalog\Model\ResourceModel\Product\Fulltext\Collection; | ||
use Smile\ElasticsuiteVirtualCategory\Model\ResourceModel\Product\CollectionFactory; | ||
|
||
/** | ||
* Apply category filter on widget collection. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteVirtualCategory | ||
* @author Pierre Gauthier <pigau@smile.fr> | ||
*/ | ||
class ProductsListPlugin | ||
{ | ||
/** | ||
* @var StoreManagerInterface | ||
*/ | ||
private $storeManager; | ||
|
||
/** | ||
* @var CategoryRepositoryInterface | ||
*/ | ||
private $categoryRepository; | ||
|
||
/** | ||
* @var Conditions | ||
*/ | ||
private $conditionsHelper; | ||
|
||
/** | ||
* ProductsListPlugin constructor. | ||
* | ||
* @param StoreManagerInterface $storeManager Store manager. | ||
* @param CategoryRepositoryInterface $categoryRepository Category repository. | ||
* @param Conditions $conditionsHelper Condition helper. | ||
*/ | ||
public function __construct( | ||
StoreManagerInterface $storeManager, | ||
CategoryRepositoryInterface $categoryRepository, | ||
Conditions $conditionsHelper | ||
) { | ||
$this->storeManager = $storeManager; | ||
$this->categoryRepository = $categoryRepository; | ||
$this->conditionsHelper = $conditionsHelper; | ||
} | ||
|
||
/** | ||
* Fix backend preview default store. | ||
* | ||
* @param ProductsList $subject Widget product list. | ||
* @return array | ||
* @throws NoSuchEntityException | ||
*/ | ||
public function beforeCreateCollection(ProductsList $subject) | ||
{ | ||
$storeId = $this->storeManager->getStore()->getId(); | ||
$subject->setData('store_id', $storeId); | ||
return []; | ||
} | ||
|
||
/** | ||
* Apply virtual category rule on widget collection. | ||
* | ||
* @param ProductsList $subject Widget product list. | ||
* @param Collection $collection Product collection. | ||
* | ||
* @return \Magento\Catalog\Model\ResourceModel\Product\Collection | ||
* @throws NoSuchEntityException | ||
*/ | ||
public function afterCreateCollection(ProductsList $subject, $collection) | ||
{ | ||
$storeId = $this->storeManager->getStore()->getId(); | ||
|
||
if ($subject->getData('condition_option') == 'condition' || !$subject->getData('condition_option')) { | ||
$conditions = $subject->getData('conditions_encoded') ?: $subject->getData('conditions'); | ||
if ($conditions) { | ||
$conditions = $this->conditionsHelper->decode($conditions); | ||
} | ||
foreach ($conditions as $condition) { | ||
if (!empty($condition['attribute'])) { | ||
if ($condition['attribute'] == 'category_ids') { | ||
if (array_key_exists('value', $condition)) { | ||
$categoryId = $condition['value']; | ||
try { | ||
$category = $this->categoryRepository->get($categoryId, $storeId); | ||
$collection->addCategoryFilter($category); | ||
} catch (NoSuchEntityException $exception) { | ||
$category = null; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return $collection; | ||
} | ||
} |
Oops, something went wrong.