Search code examples
phpmagentostoreproduct

Adding different product names for different websites programmatically


I have two websites for specific Store Views - English & German. Normally, I could have maintained 1 website with two different Store Views, but it was the specific requirement of my client, to have each website for each specific store view.

Problem is I am not able to update / create different product names / descriptions, each for a product website, programmatically. I'm using this code to do it, which I found to be the same for different price:

$combinationWebsiteWithName = array('1' => 'product name 1', '2' => 'product name 2');

foreach ($combinationWebsiteWithName as $_eachWebsiteId => $_eachProductName) {
    $objWebsite = Mage::getModel('core/website')->load($_eachWebsiteId);
    $storeIds = $objWebsite->getStoreIds();
    $objProduct = Mage::getModel('catalog/product')
                  ->setStoreId(end($storeIds))
                  ->load($productId);
    $objProduct->setName($_eachProductName);
    $objProduct->save();
}

Are there any errors in the above code?


Solution

  • Eventually, I found out what was wrong in there, and so here is the answer:-

    Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
    
    foreach ($websiteWiseProductNameArray as $_eachWebsiteId => $_eachProductName) {
        $objWebsite = Mage::getModel('core/website')->load($_eachWebsiteId);
        $storeIds = $objWebsite->getStoreIds();
    
        foreach ($storeIds as $_eachStoreId) {
            $objProduct = Mage::getModel('catalog/product')
                                ->setStoreId($_eachStoreId)
                                ->load($productId);
            $objProduct->setData($targetAttrCode, $_eachProductName);
            $objProduct->save();
    
            unset($objProduct);
        }
    
        unset($storeIds, $objWebsite);
    }
    

    Last unexpected area of modification for me was setting the store ID to be that of Admin area, by using the following code: "Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));"