EDIT: I have seen that in magento 2 the ListProduct page is cached, for this reason once loaded it does not enter.
Is there any way to modify the query returned by ListProduct?
I extend the class:
\Magento\Catalog\Block\Product\ListProduct
And the method:
addAttributeToFilter
I do not apply the changes, when I look at the products that are inside the collection, they all come out to me.
If I get the query and use it in the sql server it shows me the well filtered products.
public function getLoadedProductCollection()
{
$idCustomer = $this->getLoggedInCustomerId();
if ($idCustomer) {
$productCollection = $this->_getProductCollection();
/**
* items collection of table representative_brand
*/
$brandCollection = $this->representativeBrandCollection;
$brandCollection->addFieldToFilter(self::CUSTOMER_FIELD, $idCustomer);
$items = $brandCollection->toArray()['items'];
$arr_groups = [];
if (count($items) > 0) {
foreach ($items as $item) {
$arr_groups[] = $item[self::GROUP_FIELD];
}
}
try {
/**
* attribute: sgi_itemgroup
*/
$productCollection->addAttributeToFilter(self::GROUP_ATTRIBUTE, ['in' => $arr_groups]);
} catch (LocalizedException $e) {
$this->logger->debug('Representative Brand: ' . $e);
}
var_dump($productCollection->toArray()); die("stop");
return $productCollection;
}
return null;
}
Query of $productCollection:
SELECT
e
.*,cat_index
.position
AScat_index_position
,stock_status_index
.stock_status
ASis_salable
,price_index
.price
,price_index
.tax_class_id
,price_index
.final_price
, IF(price_index.tier_price IS NOT NULL, LEAST(price_index.min_price, price_index.tier_price), price_index.min_price) ASminimal_price
,price_index
.min_price
,price_index
.max_price
,price_index
.tier_price
,at_sgi_itemgroup
.value
ASsgi_itemgroup
FROMcatalog_product_entity
ASe
INNER JOINcatalog_category_product_index_store1
AScat_index
ON cat_index.product_id=e.entity_id AND cat_index.store_id=1 AND cat_index.visibility IN(2, 4) AND cat_index.category_id='3' AND cat_index.is_parent=1 INNER JOINcataloginventory_stock_status
ASstock_status_index
ON e.entity_id = stock_status_index.product_id AND stock_status_index.website_id = 0 AND stock_status_index.stock_id = 1 INNER JOINcatalog_product_index_price
ASprice_index
ON price_index.entity_id = e.entity_id AND price_index.website_id = '1' AND price_index.customer_group_id = '1' INNER JOINsearch_tmp_5bb499868517e4_20951822
ASsearch_result
ON e.entity_id = search_result.entity_id INNER JOINcatalog_product_entity_int
ASat_sgi_itemgroup
ON (at_sgi_itemgroup
.entity_id
=e
.entity_id
) AND (at_sgi_itemgroup
.attribute_id
= '205') AND (at_sgi_itemgroup
.store_id
= 0) WHERE (stock_status_index.stock_status = 1) AND (at_sgi_itemgroup.value IN('7', '7', '3')) ORDER BYcat_index
.position
asc,cat_index
.position
asc LIMIT 9