Question

I want to update price for particular store(us) by programmatically.I used below script and its working but problem is it updates for all stores not for particular

function updateconfigProductPrice($productsku_update,$price_to_update,$storeCode)
             {
                $proObj = getObjectInstance()->get('Magento\Catalog\Model\Product');
                if ($proObj->getIdBySku($productsku_update))
                {
                   $productId = $proObj->getIdBySku($productsku_update);
                }
                else
                {
                   return FALSE;
                }


                if($storeCode=='us')
                {
                  $storeId = '9';
                }


                //echo 'store_code'.$storeId;   

               if ($productId!= '') {
                    $configProduct = getObjectInstance()->create('Magento\Catalog\Model\Product')->load($productId);

                    //var_dump($configProduct->getWebsiteIds());
                    //die;
                    $_children = $configProduct->getTypeInstance(true)->getUsedProducts($configProduct);
                    //echo count($_children);die;

                    foreach ($_children as $child) {


                        //echo $child->getSku();die;

                        if($child->getID()!=''){

                            $childId = $child->getID();

                        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                        $productFactory = $objectManager->create('Magento\Catalog\Model\Product');
                        $productResourceModel = $objectManager->create('\Magento\Catalog\Model\ResourceModel\Product');
                        $productResourceModel->load($productFactory, $childId);
                        $productFactory->setStoreId($storeId);

                        $special_price = $price_to_update;
                        $productFactory->setPrice($special_price);
                        $productResourceModel->saveAttribute($productFactory, 'price');
                        echo $child->getSku()."child product updated for store==".$storeId."=with s Price".$special_price.".<br/>";
                        }
                    }

                }
             }

Its update price for all stores but I want to update only for my US(store). Please share your thoughts and let me know what is wrong with code.

Was it helpful?

Solution

if price update then just only one change below way then you can try it

Go to System->Configuration->Catalog->Catalog Price Scope

Then in the tab Price, set price scope to Website instead of global.

note:

If applied at the global level, the same price is used throughout the store hierarchy. If the price configuration is set to the website level, the same product can be available at different prices in different stores.

OTHER TIPS

Along with the above suggestion by Rakesh - which will fix your main issue - for the actual update it would be more efficient to use the product action class:

\Magento\Catalog\Model\Product\Action

$productIds = [1]; $data = ['price'=>12.00, 'special_price'=>10.00]; $storeId = 1;

$productAction->updateAttributes($productids, $data, $storeId);

This will save the attribute (such as price, special price, or anything else) without saving the product itself and is a lot quicker - even if you go one by one specifying the product ids.

Using update attribute works, but i cannot figure out why magento does not allow us to update price using save() on price model or productRepository.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top