3

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.

<?php
error_reporting(1);
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');
$state->setAreaCode('frontend');
$product_id = 7878;
$price = "3.99";
$productFactory = $objectManager-
>get('\Magento\Catalog\Model\ProductFactory');
$product = $productFactory->create()->load($product_id);
$product->setPrice($price);
$product->save();
?>

How can I load website?

4

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";
$productFactory = $objectManager->get('\Magento\Catalog\Model\ProductFactory');
$product = $productFactory->create()->setStoreId($storeId)->load($product_id);
$product->setPrice($price);
$product->save();

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.

| improve this answer | |
3

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

<?php
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);
        $productFactory->setStoreId($storeId);
        $productFactory->setPrice($price);
        $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;
        }

        $productFactory->setSpecialPrice($specialPrice);
        $productFactory->setSpecialFromDate($today);
        $productFactory->setSpecialFromDateIsFormated(true);
        $productFactory->setSpecialToDate($nextYear);
        $productFactory->setSpecialToDateIsFormated(true);

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

        return true;
    }
}
| improve this answer | |
  • how to update the tier price in percentage ? – MGento Feb 13 at 10:03

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.