0

I have created a custom API to update 4 attributes of the product out of which only 2 are updating not others.

I have already created attributes and assigned them to a product and working properly through the admin panel.

Attribute Code from Admin Panel

1- color
2- size
3- style
4- length

API Call in Postman

http://192.168.8.107/rest/V1/mid/updateattribute/

API attributes Data

{
    "data":{
        "sku":"MJ08-XS-Blue", 
        "color":"Blue",
        "size":"S",
        "style":"2",
        "length":"M"
    }
}

Here is the code

<?php

namespace Vendor\UpdateAttribute\Model\Api;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Psr\Log\LoggerInterface;

class Attribute
{
    protected $logger;
    
    private $_productRepository;
    public function __construct(
        LoggerInterface $logger,
        ProductRepositoryInterface $productRepository
    )
    {
        $this->logger = $logger;
        $this->_productRepository = $productRepository;
    }
    /**
     * @inheritdoc
     */
    public function updateAttribute($data)
    {
        $response = ['success' => false];   
                
        try {
            
            $productData = $this->_productRepository->get($data['sku']);
        
            // Color attribute
            $isAttrExist = $productData->getResource()->getAttribute('color'); 
            $color_id = '';
            if ($isAttrExist && $isAttrExist->usesSource()) {
                $color_id = $isAttrExist->getSource()->getOptionId($data['color']);
                $productData->setData('color', $color_id);
                $response[] = ['success' => true, 'message' => $data['color']];
            }
            else{
                $response[] = ['success' => false, 'message' => "Color " .$data['color']. " not available"];
            }
            
            // Size attribute
            $isAttrExist = $productData->getResource()->getAttribute('size'); 
            $size_id = '';
            if ($isAttrExist && $isAttrExist->usesSource()) {
                $size_id = $isAttrExist->getSource()->getOptionId($data['size']);
                $productData->setData('size', $size_id);
                $response[] = ['success' => true, 'message' => $data['size']];
            }
            else{
                $response[] = ['success' => false, 'message' => "Size ". $data['size']. " not available"];
            }
            
            // Style attribute
            $isAttrExist = $productData->getResource()->getAttribute('style'); 
            $style_id = '';
            if ($isAttrExist && $isAttrExist->usesSource()) {
                $style_id = $isAttrExist->getSource()->getOptionId($data['style']);
                $productData->setData('Style', $style_id);
                $response[] = ['success' => true, 'message' => $data['style']];
            }
            else{
                $response[] = ['success' => false, 'message' => $data['style']." not available"];
            }
            
            // Length attribute
            $isAttrExist = $productData->getResource()->getAttribute('length'); 
            $length_id = '';
            if ($isAttrExist && $isAttrExist->usesSource()) {
                $length_id = $isAttrExist->getSource()->getOptionId($data['length']);
                $productData->setData('length', $length_id);
                $response[] = ['success' => true, 'message' => $data['length']];
            }
            else{
                $response[] = ['success' => false, 'message' => "Length " .$data['length']." not available"];           }       
            
            $productData->save($productData);
            //$objectManager->get('Magento\Catalog\Api\ProductRepositoryInterface')->save($productData);
            
            $response = ['success' => true, 'message' => $data['sku']];
        } catch (\Exception $e) {
            $response = ['success' => false, 'message' => $e->getMessage()];
            $this->logger->info($e->getMessage());
        }
        $returnArray = json_encode($response);
        return $returnArray; 
   }
}

Code is working for 2 attributes only

1- color
2- size

While other 2 attibutes not updating

3- style
4- length
2
  • check the attribute type of style and length, they are input text or option dropdown..? Commented Mar 11, 2022 at 18:22
  • all are dropdowns, the color attribute is using swatch also and it is also updating.
    – ishaq
    Commented Mar 11, 2022 at 19:05

1 Answer 1

0

After comparing attributes settings found that the attribute which was not updating had scope "Website" after changing the scope to "Global" it is working fine.

enter image description here

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.