Question

I'm trying to get a list of products with a custom attribute set a certain way. Right now only one product should be returned, however it's returning the whole catalog. It seems like my added attributes are just being ignored. Any idea what I'm doing wrong?

This is my block file:
namespace Mycompany\MyModule\Block\Index;

class Index extends \Magento\Framework\View\Element\Template
{
    protected $product;
    protected $_storeManager;

    /**
     * Constructor
     *
     * @param \Magento\Framework\View\Element\Template\Context  $context
     * @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $product
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $product,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        array $data = []
    ) {
        $this->product = $product;
        $this->_storeManager = $storeManager;
        parent::__construct($context, $data);
    }

    public function getProducts()
    {

        $collection = $this->product->create()->setStoreId($this->_storeManager->getStore()->getId());
        $collection->addAttributeToSelect('*')
            ->addAttributeToFilter('recalled', ['eq'=>'1'])
            ->addAttributeToFilter('status', ['eq'=>'2']);

        print_r($collection->getSelect()->__toString());
        die();

        return $collection;
    }
}

And this is the SQL it spits out:

SELECT 1 AS `status`, `e`.`entity_id`, `e`.`attribute_set_id`, `e`.`type_id`, `e`.`created_at`, `e`.`updated_at`, `e`.`sku`, `e`.`entity_id`, `e`.`attribute_set_id`, `e`.`type_id`, `e`.`row_id`, `e`.`allow_open_amount`, `e`.`b2c_fashion`, `e`.`b2c_fashion_value`, `e`.`cost`, `e`.`created_at`, `e`.`description`, `e`.`email_template`, `e`.`giftcard_amounts`, `e`.`giftcard_type`, `e`.`gift_message_available`, `e`.`gift_wrapping_available`, `e`.`gift_wrapping_price`, `e`.`has_options`, `e`.`image`, `e`.`image_label`, `e`.`is_redeemable`, `e`.`item_number`, `e`.`lifetime`, `e`.`links_exist`, `e`.`links_purchased_separately`, `e`.`links_title`, `e`.`msrp`, `e`.`msrp_display_actual_price_type`, `e`.`name`, `e`.`news_from_date`, `e`.`news_to_date`, `e`.`open_amount_max`, `e`.`open_amount_min`, `e`.`price`, `e`.`price_type`, `e`.`price_view`, `e`.`required_options`, `e`.`short_description`, `e`.`sku`, `e`.`sku_type`, `e`.`small_image`, `e`.`small_image_label`, `e`.`special_from_date`, `e`.`special_price`, `e`.`special_to_date`, `e`.`swatch_image`, `e`.`tax_class_id`, `e`.`thumbnail`, `e`.`thumbnail_label`, `e`.`ts_dimensions_height`, `e`.`ts_dimensions_length`, `e`.`ts_dimensions_width`, `e`.`upc_code`, `e`.`updated_at`, `e`.`url_key`, `e`.`url_path`, `e`.`use_config_email_template`, `e`.`use_config_is_redeemable`, `e`.`use_config_lifetime`, `e`.`visibility`, `e`.`weight`, `e`.`weight_type` FROM `catalog_product_flat_1` AS `e`
Was it helpful?

Solution 2

A combination of di:compile and clearing cache seemed to have magically fixed it. I also changed the status line to

->addAttributeToFilter('status', \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED);

But pretty sure that didn't fix anything.

OTHER TIPS

Try to not set numbers between quotes :

public function getProducts()
    {

        $collection = $this->product->create()->setStoreId($this->_storeManager->getStore()->getId());
        $collection->addAttributeToSelect('*')
            ->addAttributeToFilter('recalled', ['eq'=> 1])
            ->addAttributeToFilter('status', ['eq'=> 2]);

        print_r($collection->getSelect()->__toString());
        die();

        return $collection;
    }

Try this

$collection->addAttributeToSelect('*')
       ->addAttributeToFilter('recalled', 1)
       ->addAttributeToFilter('status', 2);
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top