Update product attribute value programmatically in Magento 2 .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
<?php namespace Vendore\Module\Helper; use Magento\Catalog\Model\Product\Action as ProductAction; use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; use Magento\Framework\App\Helper\AbstractHelper; use Magento\Framework\App\Helper\Context; use Magento\Store\Model\StoreManagerInterface; 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()); } } } |
If you are facing error as per the below, Then...
I've to add custom CSS for particular store on checkout...
If we use the quote collectTotal() method in our custom...
If you want to set a different look for a...
I have recently faced an issue during CSV import as...