Try the Demo

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:  2168
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.

 Signature 

http://marius-strajeru.blogspot.com/
Check out the Ultimate Module Creator:
on magento connect
on github

 
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