I tried to use the addAttributeToFilter
like the example below:
$_productCollection = $block->getLoadedProductCollection()->addAttributeToFilter('discontinued', 1);
echo($_productCollection->getSize());
echo($_productCollection->count());
foreach ($_productCollection as $product) {
echo($product->getName());
}
The attribute discontinued
is a yes/no
field. The origin $_productCollection
has two products and one of the product's attribute discontinued
is set to be yes
.
The first two echo are 1
, but the echo in the loop prints productName1
and productName2
. I think the filter should be working but somehow when I traverse the for loop, all products are printed out. How is this supposed to happen?