1

I am writing an import script that is going to loop through some records and add products. I have it adding the products nicely but my store is multilingual so each product needs and english or a spanish name depending on which store is being viewed.

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product = Mage::getModel('catalog/product');
try{
$product


    ->setWebsiteIds(array(1))
    ->setStoreId(0)
    ->setAttributeSetId(4)
    ->setTypeId('bundle')
    ->setName($en_title)
    ->setCreatedAt(strtotime('now'))
    ->setSku('resource'.$resource_id)
    ->setStatus(1)
    ->setTaxClassId(0)
    ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
    ->setPrice(number_format($price, 2, '.', ''))
    ->setStockData(array(
                       'use_config_manage_stock' => 0,
                       'manage_stock'=>0,
                   )
    )
    ->setCategoryIds(array(51))
    //->save()
    ->setStoreId(1)
    ->setName($en_title)
    //->save()
    ->setStoreId(2)
    ->setName($es_title)
    ->save();
}catch(Exception $e){
Mage::log($e->getMessage());
}

My English store had an ID of 1, and my Spanish store has an ID of 2. When I use the posted code all names are in Spanish regardless of store, and when I uncomment the save functions then all names are in English. I can't figure out why it wont save my Spanish name under my spanish store and my English name as Default and under the English store.

0

Try this:

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product = Mage::getModel('catalog/product');
try{
   $product
      //set all attributes here as you already do
      //....
   ;
   $product->save();
   //now for the spanish store
   //get the id of the product you just created
   $productId = $product->getId();
   //load the product in the ES store
   $esProduct = Mage::getModel('catalog/product')->setStoreId(2)->load($productId);
   //set the values you need
   $esProduct->setName('name in es');
   //.... 
   $esProduct->save();
}
catch (Exception $e) {
    //do something if there is an error
}

An other faster way of doing it is, after adding the product with the default values, just update certain attributes that are different in ES.

So the first part is the same

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product = Mage::getModel('catalog/product');
try{
   $product
      //set all attributes here as you already do
      //....
   ;
   $product->save();
   //now for the spanish store
   //get the id of the product you just created
   $productId = $product->getId();

   //now update the attribute for ES.  

   Mage::getSingleton('catalog/product_action')->updateAttributes(
       array($productId), //an array with the id you get from above
       array(  //array with all the attributes to update 'key'=>'value'
           'name' => 'Name in Es',
           'description' => 'Description in Es',
           ....
       ), 
       2 //the id of the ES store
   );
catch (Exception $e) {
       //do something when an exception occurs.
}
1

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.