Filtering works, but it breaks the number of items on the page (products hidden by the filter disappear), for example, was 12, the filter on the page found 2, removed them and now the page collection only 10 products, although the page size is set to 12. Plus, the toolbar shows the old number without the filter.
What are better ways to do this?
<?php
namespace VendorModulePlugin;
use AmastyShopbyModelResourceModelFulltextCollection;
use MagentoFrameworkAppArea;
use MagentoFrameworkAppConfigScopeConfigInterface;
use MagentoFrameworkAppState;
use MagentoFrameworkViewLayoutInterface;
use MagentoStoreModelScopeInterface;
class Load
{
protected $state;
protected $layout;
protected $scopeConfig;
const XML_PATH_CATEGORY = 'idea_brand_filters/brand_filters/category';
const XML_PATH_LAYOUT_CHECK = 'idea_brand_filters/brand_filters/layout_check';
public function __construct(
State $state,
LayoutInterface $layout,
ScopeConfigInterface $scopeConfig,
) {
$this->state = $state;
$this->layout = $layout;
$this->scopeConfig = $scopeConfig;
}
public function beforeLoad(Collection $subject, $printQuery = false, $logQuery = false)
{
if ($this->isBrandLayoutCheckEnabled()) {
if ($this->isBrandLayoutCheckEnabled() && $this->state->getAreaCode() == Area::AREA_FRONTEND) {
$layout = $this->layout->getUpdate()->getHandles();
if (in_array('ambrand_index_index', $layout)) {
$subject->addCategoriesFilter(["nin" => $this->getHiddenCategories()]);
}
}
} else {
$subject->addCategoriesFilter(["nin" => $this->getHiddenCategories()]);
}
return [$printQuery, $logQuery];
}
public function getHiddenCategories()
{
$ids = $this->scopeConfig->getValue(self::XML_PATH_CATEGORY, ScopeInterface::SCOPE_STORE);
if ($ids) {
return explode(',', $ids);
} else {
return [];
}
}
public function isBrandLayoutCheckEnabled()
{
return $this->scopeConfig->getValue(self::XML_PATH_LAYOUT_CHECK, ScopeInterface::SCOPE_STORE);
}
}