2

I am unable to save product multiselect values .I tried both ways Here is code

Way 1

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $_product = $objectManager->create('Magento\Catalog\Model\Product');
        $_product->load(757);
        $string = '5641,5642'; // option ids
     $_product->setData('multi_select_color_code',$string);
                $_product->save();

Way 2:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $_product = $objectManager->create('Magento\Catalog\Model\Product');
        $_product->load(757);
        $array= array('5641','5642'); // option ids
     $_product->setData('multi_select_color_code',$array);
                $_product->save();
0

3 Answers 3

4

Try this code

protected $productRepository;

public function __construct(
    .........................................................
    \Magento\Catalog\Model\ProductRepository $productRepository,
    .........................................................
) {
    .........................................................
    $this->productRepository = $productRepository;
    .........................................................
}
public function SetProductAttribute()
{
    $productId = "757";
    $attributeValue = '5641,5642';
    $product = $this->productRepository->getById($productId);
    $product->setCustomAttribute('multi_select_color_code', $attributeValue);
    //$product->setData('multi_select_color_code', $attributeValue);
    $this->productRepository->save($product);
}

I Hope This Helps You

6
  • thank you for your answer. After digging into database table catalog_product_entity_varchar .my data was storing as store id 1 so thats why it did not reflect on admin products. after putting $product->setStoreId(0); it reflects the data in admin as well as frontend. Thank you for your answer Commented Jul 17, 2020 at 14:15
  • please tell me this solution is works for you ?
    – Msquare
    Commented Jul 20, 2020 at 5:13
  • No bro ,but I did up vote for your answer. Thank you Commented Jul 20, 2020 at 13:10
  • But bro this answer is not useful to solved my problem,how can I accepted as green tick mark ? Commented Jul 20, 2020 at 13:16
  • so please tell me what error you are facing
    – Msquare
    Commented Jul 20, 2020 at 13:34
1

Finally I solved my problem by putting

$product->setStoreId(0)
0

I have the class below working at my end. In other words, way 2 is the way to go but this is assuming your custom attribute has the right setup. Also, you may notice below I am setting the area code to adminhtml

 class SaveProductAttribute extends Command
    {
        /**
         * @var ProductRepositoryInterface
         */
        private $productRepository;
        /**
         * @var \Magento\Framework\App\State
         */
        private $state;
    
        public function __construct(
            ProductRepositoryInterface $productRepository,
            \Magento\Framework\App\State $state,
            $name = null)
        {
            parent::__construct($name);
            $this->productRepository = $productRepository;
            $this->state = $state;
        }
    
        public function configure()
        {
            $this->setName('mbs:product_multiselect:save')
                ->setDescription('Save product multiselect value');
    
            $this->addArgument('sku', InputArgument::REQUIRED);
        }
    
        protected function execute(InputInterface $input, OutputInterface $output)
        {
            $this->initialiseAreaCode();
    
            $product = $this->productRepository->get($input->getArgument('sku'));
    
            $output->writeln(sprintf('sku: %s has activity: %s',
                $input->getArgument('sku'),
                $product->getData('activity'))
            );
    
            $product->setData('activity', [5432]);
            $this->productRepository->save($product);
    
            $output->writeln('attribute saved');
        }
    
        private function initialiseAreaCode(): void
        {
            try {
                $this->state->setAreaCode(Area::AREA_ADMINHTML);
            } catch (\Exception $e) {
    
            }
        }
1
  • thank you for your answer. After digging into database table catalog_product_entity_varchar .my data was storing as store id 1 so thats why it did not reflect on admin products. after putting $product->setStoreId(0); it reflects the data in admin as well as frontend. Thank you for your answer Commented Jul 17, 2020 at 14:15

Your Answer

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

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