We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

    Update product attribute value programmatically in Magento 2 .

    <?php
    
    namespace VendoreModuleHelper;
    
    use MagentoCatalogModelProductAction as ProductAction;
    use MagentoCatalogModelResourceModelProductCollectionFactory;
    use MagentoFrameworkAppHelperAbstractHelper;
    use MagentoFrameworkAppHelperContext;
    use MagentoStoreModelStoreManagerInterface;
    
    class Data extends AbstractHelper
    {
        protected $messageManager;
        private $productCollection;
        private $productAction;
        private $storeManager;
    
        public function __construct(
            Context $context,
            CollectionFactory $collection,
            ProductAction $action,
            StoreManagerInterface $storeManager
        )
        {
            $this->productCollection = $collection;
            $this->productAction = $action;
            $this->storeManager = $storeManager;
            parent::__construct($context);
        }
    
        public function setcustomAttributeData($param)
        {
            try {
                $collection = $this->productCollection->create()->addFieldToFilter('*');
                $storeId = $this->storeManager->getStore()->getId();
                $ids = [];
                $i = 0;
                foreach ($collection as $item) {
                    $ids[$i] = $item->getEntityId();
                    $i++;
                }
                $this->productAction->updateAttributes($ids, array('attribute' => $param), $storeId);
    
            } catch (Exception $e) {
                $this->messageManager->addError($e->getMessage());
            }
        }
    }

     

    Fill the below form if you need any Magento relate help/advise/consulting.

    With Only Agency that provides a 24/7 emergency support.

      Get a Free Quote