0

I am using Magento2. There are many products which have no attribute is_imported and is_csv_imported. I want to get and delete all these products.

Here is my custom script:

<?php
use Magento\Framework\App\Bootstrap;

require __DIR__ . '/../app/bootstrap.php';

$params = $_SERVER;

$bootstrap = Bootstrap::create(BP, $params);

$objectManager = $bootstrap->getObjectManager();



$productcollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
$productcollection->addAttributeToSelect('*');
$productcollection->addAttributeToFilter('is_imported', array('neq' => 1));
$productcollection->addAttributeToFilter('is_csv_imported', array('neq' => 1));



foreach ($productcollection as $product){
    print_r($product->getId());
}

?>
2
  • What is your problem? – Tokey Tu Aug 26 '19 at 10:04
  • not returns products which have no these attributes – Dito Khelaia Aug 26 '19 at 10:25
0
<?php
use Magento\Framework\App\Bootstrap;

require __DIR__ . '/../app/bootstrap.php';

$params = $_SERVER;

$bootstrap = Bootstrap::create(BP, $params);

$objectManager = $bootstrap->getObjectManager();



$productcollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
$productcollection->addAttributeToSelect('*');
$productcollection->addAttributeToFilter('is_imported', [['null' => true],['neq' => 1]], "left");
$productcollection->addAttributeToFilter('is_csv_imported', [['null' => true],['neq' => 1]], "left");



foreach ($productcollection as $product){
    print_r($product->getId());
}

?>

Always use left join if you filter data with not equal statements. Otherwise - you won't get any rows without any attribute value. Your original query was getting all products where attributes were actually not null and not equal 1.

2
  • returned 3k products there are also other 3k – Dito Khelaia Aug 26 '19 at 11:35
  • Print the query by adding line: echo $productcollection->getSelect(); die; before foreach loop. – dudzio Aug 26 '19 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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