Wednesday , December 19 2018
Home >> All tutorial >> [Magento Tutorial] – Updating Products Within Website Scope

[Magento Tutorial] – Updating Products Within Website Scope

Sometimes in Magento you need to write some code to update products programmatically. This may go hand in hand with wanting to batch update products within a certain store or website scope. Recently we needed to be able to specify which websites should be updated with the ‘price’ attribute. A couple issues stood in our way.

The first being that the scope of all price attributes (price, special price, special from date, special to date, etc.) were not managed in the ‘Manage Attributes’ section in the Magento backend as one would assume. Instead, they can be found in System > Configuration > Catalog > Catalog > Price > Price Scope. You can either choose ‘Global’ (meaning across all websites) or ‘Website’ (for website specific pricing, as we are wanting.) We cannot choose ‘Store’ scope.

We can now use the following code in a function to adjust the price programatically for specific website codes. You’ll also notice below that we set the store to the admin store, something I struggled with. We must set the admin store first, and then set the scope of the specific product later ($product->setStoreId($store->getStoreId());) You may also notice lines like this: $product->setSpecialToDateIsFormated(true);. We need these to pass in a date as a string and tell Magento that it is indeed a date and should be stored that way.

{code type=xml}
public function updateSpecialPriceByWebsite($price, $specialFromDate, $specialToDate, $sku, $website)
try {
$specialFromDate = date(‘Y-m-d’, strtotime($specialFromDate));
$specialToDate = date(‘Y-m-d’, strtotime($specialToDate));
$website = Mage::app()->getWebsite($website);
$store = $website->getDefaultStore();
$product = Mage::getModel(‘catalog/product’);
$productId = $product->getIdBySku($sku);
if ($product && $product->getId()) {
return true;
} catch (Exception $e) {
return false;

return true;

Here is the library of Magento Download for FREE (all versions)

Your Magento website needs a powerful Magento ERP for stock control.


magento erp

About Champ

Leave a Reply

Your email address will not be published. Required fields are marked *

Heap | Mobile and Web Analytics