I try to programmatically update product price for different website in magento2.

This blow code is working but It will update main website price only, I need to update different website price using different website id.

ini_set('max_execution_time', 0);
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$state = $objectManager->get('\Magento\Framework\App\State');
$product_id = 7878;
$price = "3.99";
$productFactory = $objectManager-
$product = $productFactory->create()->load($product_id);

How can I load website?

2 Answers 2


You need to set store id when you load product object, Your final code look like this

$storeId = '1'; //Store ID
$product_id = 7878;
$price = "3.99";
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productFactory = $objectManager->get('\Magento\Catalog\Model\ProductFactory');
$product = $productFactory->create()->setStoreId($storeId)->load($product_id);

Also, you need to change price scope.

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

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

  • @prince any idea about the special price? how we can set it? Jan 24, 2022 at 11:12

Well, That's how I got it working on Magento 2.2.3+

namespace Vendor\Module\Helper;

class Product {

    protected $productResourceModel;
    protected $productFactory;

    public function __construct(
        \Magento\Catalog\Model\ResourceModel\Product $productResourceModel,
        \Magento\Catalog\Model\ProductFactory $productFactory
    ) {
        $this->productResourceModel = $productResourceModel;
        $this->productFactory = $productFactory;

    public function setPricesPerStore($productId, $storeId, $price, $specialPrice)
        $productFactory = $this->productFactory->create();
        $this->productResourceModel->load($productFactory, $productId);
        $this->productResourceModel->saveAttribute($productFactory, 'price');

        $today = date('Y-m-d', time());
        $nextYear = date('Y-m-d', time() + (60*60*24*7*52));

        if(!$specialPrice || ($price == $specialPrice)) {
            $specialPrice = null;
            $today = null;
            $nextYear = null;


        $this->productResourceModel->saveAttribute($productFactory, 'special_price');
        $this->productResourceModel->saveAttribute($productFactory, 'special_from_date');
        $this->productResourceModel->saveAttribute($productFactory, 'special_to_date');

        return true;
  • how to update the tier price in percentage ?
    – MGento
    Feb 13, 2020 at 10:03

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.