1

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.

  • What do you need the loop "availableWebsites" for? You don't use the param "websiteId"? – raumatbel Jul 15 '18 at 19:12
  • @raumatbel Its to check for available websites for current product. I am using websiteId to match with store website which code is not related to the issue. – Jaimin Sutariya Jul 16 '18 at 4:50
4

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

2

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());
            }
        }
    }
}
  • Thanks Nikin for the answer. I have tried it too but was not able to resolve the issue. – Jaimin Sutariya Jul 17 '18 at 4:46
  • Looked at the solution you have shared. Glad that you resolved it on your own. – nikin Jul 17 '18 at 6:52

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.