0

In Magento 2,

How to do a mass deletion of all products attribute values, by attribute_code.

For exemple I want to delete all values for attribute_code = "my_code".

EDIT

I also want to update products attribute values by attribute_code.

I found this :

$productActionObject = $objectManager->create('Magento\Catalog\Model\Product\Action');
$productActionObject->updateAttributes($idArray, array('weight' => 15), 0); 

But my weight values are different between each product ids. I want to update attribute according to an array with product_id as key and weight as value:

$array[1] = 14;
$array[2] = 152;
$array[3] = 58;
...

I can't load every products, because there is a lot of values.

Thansk for your help

0

Try This

protected $_productCollectionFactory;
protected $_productRepository;

public function __construct(
    .......................................................
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
    \Magento\Catalog\Model\ProductRepository $productRepository,
    .......................................................

) {
    .......................................................
    $this->_productCollectionFactory = $productCollectionFactory;
    $this->_productRepository = $productRepository;
    .......................................................
}

public function execute()
{
    $prdcollection = $this->_productCollectionFactory->create();
    $attributeCode = "Attribute_code";
    $attributeValue = "Attribute_Value"
    foreach ($prdcollection as $prdkey => $prdvalue) {
        $prdData = $this->_productRepository->getById($prdvalue->getId());
        $prdData->setCustomAttribute($attributeCode, $attributeValue);
        //$prdData->setData($attributeCode, $attributeValue);
        $this->_productRepository->save($prdData);
    }
}
2
  • Thanks but it's not a solution for me. I don't want to make a one shot script. But it's an import script run every day with a cronjob.
    – Fbr
    Nov 23 '20 at 10:43
  • Please check my update answer. and update me.
    – Msquare
    Nov 23 '20 at 11:20

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.