Magento 2 Programatically update price attribututes per store but code is updating for all stores












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('MagentoCatalogModelProduct');
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('MagentoCatalogModelProduct')->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 = MagentoFrameworkAppObjectManager::getInstance();
$productFactory = $objectManager->create('MagentoCatalogModelProduct');
$productResourceModel = $objectManager->create('MagentoCatalogModelResourceModelProduct');
$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.









share





























    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('MagentoCatalogModelProduct');
    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('MagentoCatalogModelProduct')->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 = MagentoFrameworkAppObjectManager::getInstance();
    $productFactory = $objectManager->create('MagentoCatalogModelProduct');
    $productResourceModel = $objectManager->create('MagentoCatalogModelResourceModelProduct');
    $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.









    share



























      0












      0








      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('MagentoCatalogModelProduct');
      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('MagentoCatalogModelProduct')->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 = MagentoFrameworkAppObjectManager::getInstance();
      $productFactory = $objectManager->create('MagentoCatalogModelProduct');
      $productResourceModel = $objectManager->create('MagentoCatalogModelResourceModelProduct');
      $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.









      share
















      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('MagentoCatalogModelProduct');
      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('MagentoCatalogModelProduct')->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 = MagentoFrameworkAppObjectManager::getInstance();
      $productFactory = $objectManager->create('MagentoCatalogModelProduct');
      $productResourceModel = $objectManager->create('MagentoCatalogModelResourceModelProduct');
      $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.







      magento2 configurable-product price





      share














      share












      share



      share








      edited 4 mins ago









      HIren Kadivar

      370110




      370110










      asked 9 mins ago









      akgolaakgola

      1,357518




      1,357518






















          1 Answer
          1






          active

          oldest

          votes


















          0














          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.





          share
























          • Will it work without effecting to other stores?

            – akgola
            3 mins ago











          • yes working not effecting another store

            – Rakesh Donga
            2 mins ago











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "479"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f260472%2fmagento-2-programatically-update-price-attribututes-per-store-but-code-is-updati%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          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.





          share
























          • Will it work without effecting to other stores?

            – akgola
            3 mins ago











          • yes working not effecting another store

            – Rakesh Donga
            2 mins ago
















          0














          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.





          share
























          • Will it work without effecting to other stores?

            – akgola
            3 mins ago











          • yes working not effecting another store

            – Rakesh Donga
            2 mins ago














          0












          0








          0







          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.





          share













          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.






          share











          share


          share










          answered 4 mins ago









          Rakesh DongaRakesh Donga

          1,041212




          1,041212













          • Will it work without effecting to other stores?

            – akgola
            3 mins ago











          • yes working not effecting another store

            – Rakesh Donga
            2 mins ago



















          • Will it work without effecting to other stores?

            – akgola
            3 mins ago











          • yes working not effecting another store

            – Rakesh Donga
            2 mins ago

















          Will it work without effecting to other stores?

          – akgola
          3 mins ago





          Will it work without effecting to other stores?

          – akgola
          3 mins ago













          yes working not effecting another store

          – Rakesh Donga
          2 mins ago





          yes working not effecting another store

          – Rakesh Donga
          2 mins ago


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Magento Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f260472%2fmagento-2-programatically-update-price-attribututes-per-store-but-code-is-updati%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Magento 2 controller redirect on button click in phtml file

          Polycentropodidae