Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

programmaticaly save product into different stores
 
jozeflukac
Jr. Member
 
Total Posts:  3
Joined:  2012-08-13
 

Hi folks,

I’ve got a problem with saving a product into different stores.
We use this stores as translations (en and sk).  If I do:

$product->setName(’Some English name’);
$product->setDescription(’Some English description’);
$product->setStoreId(1);
$product->save();

$product->setName(’Some Slovak name’);
$product->setDescription(’Some Slovak description’);
$product->setStoreId(2);
$product->save();

It says, that I’ve got a duplicate ID(sku). :((
Can you help me please? Thanks a lot in advance.

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

Hello.
First you have to load the product then save it.
In your case Magento thinks you are adding a new product.

Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
//the line above is needed for Magento versions 1.4 and later. The product saving works only if you are in admin. The line emulates the admin.

$productId 5;
$product Mage::getModel('catalog/product')->setStoreId(1)->load($productId);
//if you don't have the product Id and you want to work with sku ignore the line above and use the following 3 lines
//$sku = '12345';
//$productId = Mage::getModel('catalog/product')->getIdBySku($sku);
//$product = Mage::getModel('catalog/product')->setStoreId(1)->load($productId);

$product->setName(’Some English name’); 
$product->setDescription(’Some English description’); 
$product->save();

You need to follow the sequence above for all the stores for which you want different values.
Cheers,
Marius.

 
Magento Community Magento Community
Magento Community
Magento Community
 
jozeflukac
Jr. Member
 
Total Posts:  3
Joined:  2012-08-13
 

Thanks folk, worked like a charm smile)

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top