0

I have added 2 fields in db_schema.xml there created on table catalog_product_entity the challenges for me now are I'm using the one field as custom state and this state is the field I need to filter on.

here is my code

$products = $this->productCollection->create()
            ->addAttributeToSelect('*')
            ->addAttributeToFilter([
                [
                    'attribute' => 'status',
                    'eq' => \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED
                ],
            ])
            ->setPageSize($page_size)
            ->setCurPage($current_page);

The problem is coming when I'm adding the next filter like this

->addAttributeToFilter([
                    [
                        'attribute' => 'status',
                        'eq' => \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED
                    ],
[
                        'attribute' => 'my_state',
                        'nin' => 'custom-state'
                    ],
                ])

When I did it this way its tell me the attribute is invalid, but its exists in the table, I have doublet check my database tool

The "my_state" attribute name is invalid. Reset the name and try again

1 Answer 1

0

Remove that newly added column filter from attribute filter as it's not attribute it's a field you have added in main catalog entity table and add the field filter condition.

So instead of

->addAttributeToFilter([
                [
                    'attribute' => 'status',
                    'eq' => \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED
                ],
                [
                    'attribute' => 'my_state',
                    'nin' => 'custom-state'
                ],
    ])

Change it to

->addAttributeToFilter([
                [
                    'attribute' => 'status',
                    'eq' => \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED
                ]
            ]);
->addFieldToFilter('my_state', 'nin'=>['custom-state']);

Upvote if it works!

8
  • Its not working, still say its can't find my field, and its exists in my table: /var/www/project # bin/magento smartpack:product:sync In AbstractCollection.php line 1407: The "wms_state" attribute name is invalid. Reset the name and try again. Aug 19, 2021 at 20:17
  • You have used that wms_state in ->addFieldToFilter('my_state', 'nin'=>['wms_state']); like this right?
    – Deep Joshi
    Aug 20, 2021 at 13:50
  • Also have you removed that from addAttributeToFilter? Try to add addFieldToSelect('wms_state')
    – Deep Joshi
    Aug 20, 2021 at 13:51
  • When i add the "addFieldToSelect" its working so much I can see it in the output but same error when I add the "filter" function again, but one step closer Aug 21, 2021 at 9:10
  • Use should use addFieldToSelect('wms_state') first and then after add that filter condition by that way it should work.
    – Deep Joshi
    Aug 23, 2021 at 8:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.