0

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.

3

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.

| improve this answer | |
  • Will it work without effecting to other stores? – akgola Feb 5 '19 at 6:24
  • yes working not effecting another store – Rakesh Donga Feb 5 '19 at 6:25
  • Ok thanks,I am trying.why this option is not enabled default? – akgola Feb 5 '19 at 6:35
  • i have updated note in my answer – Rakesh Donga Feb 5 '19 at 7:14
  • I created custom API to update details but I want to update detail based on store code and store code is collected from payload any idea? – Yo Yo Roshan May 14 at 7:26
1

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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