Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Virtual Category] Pagebuilder Compatibility #ESP-286 #2304

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -334,15 +334,12 @@ public function getFacetedData($field)
*/
public function addCategoryFilter(\Magento\Catalog\Model\Category $category)
{
$categoryId = $category;

if (is_object($category)) {
$categoryId = $category->getId();
$categoryId = $category->getId();
if ($categoryId) {
$this->addFieldToFilter('category_ids', $categoryId);
$this->_productLimitationFilters['category_ids'] = $categoryId;
}

$this->addFieldToFilter('category_ids', $categoryId);
$this->_productLimitationFilters['category_ids'] = $categoryId;

return $this;
}

Expand Down
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);
}
}
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;
}
}
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);
}
}
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];
}
}
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;
}
}
Loading