Read More
Read More
Menu Close

    Mass Update Products attribute in Magento 2

    In this post, we will see How we can Update Products attribute for a large number of products in most optimised way possible. Suppose, you have added a product attribute with attribute code ‘wk_shipping’. You want to set 50 unit to this for a large number of products.

    Use the following code to Save attribute value for Products:

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $array_product = [1,2,3,4,5]; //product Ids
    $value = 50; //amount
    $productActionObject = $objectManager->create('Magento\Catalog\Model\Product\Action');
    $productActionObject->updateAttributes($array_product, array('wk_shipping' => $value), 0);
    

    The function updateAttributes prototype is as such:
    /**
    * Update attribute values for entity list per store
    *
    * @param array $productIds
    * @param array $attrData
    * @param int $storeId
    * @return $this
    */
    public function updateAttributes($productIds, $attrData, $storeId);

    I have tried to make a standalone script which is ready to use. You can improve code by directly injecting the dependency of this class Magento\Catalog\Model\Product\Action in constructor. Hope, this may be helpful in your journey with Magento 2. For any query, do let me know in the comment below. Thanks 🙂

    Searching for an experienced
    Magento 2 Company ?
    Read More
    . . .
    Discuss on Helpdesk

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top