Question

I have set catalog price scope as Website in my store configuration to add different prices for different websites.

Now I am creating products using script which contains price data per website, but I am not able to store different prices for different websites scopes.

I am using below code. Using Reference

$product // << My Product Object
$availableWebsites // << Available Websites for product

foreach($availableWebsites as $websiteId){
    foreach($attributesDataArray as $storeId => $storeData){ // Loop through all available Stores
        $product->setStoreId($storeId);
        $product->setPrice($price); // Set My price. Different for each website
        try{
            $product->save();
        }catch(\Exception $e){
            throw new \Exception($e->getMessage());
        }
    }
}

But the price is stored the same for each website scope. Any help is appreciated. Thanks.

Was it helpful?

Solution

I was able to resolve my problem with below code. I used saveAttribute method to save attribute values (price, special price, etc.) which will only save attribute value to database without saving complete product.

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

public function setPriceForWebsite(){
    .....
    $product // << My Product Object
    $productFactory = $this->productFactory->create();
    $this->productResourceModel->load($productFactory, $product->getId());
    $availableWebsites // << Available Websites for product

    foreach($availableWebsites as $websiteId){
        foreach($attributesDataArray as $storeId => $storeData){ // Loop through all available Stores
            $product->setStoreId($storeId);
            try{
                $productFactory->setPrice($price);
                $this->productResourceModel->saveAttribute($productFactory, 'price'); // Set My price. Different for each website
            }catch(\Exception $e){
                throw new \Exception($e->getMessage());
            }
        }
    }
}

Reference

OTHER TIPS

Check this code.

Try loading product $prod by setting store ID and product ID from $product object.

protected $productRepository;

public function __construct(
   Magento\Catalog\Model\ProductRepository $productRepository
) {
   $this->productRepository = $productRepository;
}

public function updateProductPricesByStoreId($productId)
{
    $product // << My Product Object
    $availableWebsites // << Available Websites for product

    foreach($availableWebsites as $websiteId){
        foreach($attributesDataArray as $storeId => $storeData){ // Loop through all available Stores
            $prod = $this->productRepository->getById($product->getId(), false, $storeId);
            $prod->setPrice($price); // Set My price. Different for each website
            try{
                $product->save();
            }catch(\Exception $e){
                throw new \Exception($e->getMessage());
            }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top