Magento 1.9 – Programmatically Create Product with Different Attributes in Store Views

importmagento-1.9multistoreproduct

I have problem ,while i programmatically create product. I want add two different values for store views

$product = Mage::getModel('catalog/product');
$product->setStoreId(1)->setData('name', 'English name')->save();
$product->setStoreId(2)->setData('name', 'Polish name')->save();

After those operation I have proper data in catalog_product_entity_varchar

┌────────┬──────────────┬────────────┬────────┬─────────┬────────────┐
│value_id│entity_type_id│attribute_id│store_id│entity_id│value       │
├────────┼──────────────┼────────────┼────────┼─────────┼────────────┤
│1       │10            │96          │0       │1        │English name│
├────────┼──────────────┼────────────┼────────┼─────────┼────────────┤
│6       │10            │96          │2       │1        │Polish name │
└────────┴──────────────┴────────────┴────────┴─────────┴────────────┘

And this data are good…..

BUT if I want add description (the same way) after name

$product->setStoreId(1)->setData('description', 'English')->save();
$product->setStoreId(2)->setData('description', 'Polish')->save();

catalog_product_entity_varchar change to:

┌────────┬──────────────┬────────────┬────────┬─────────┬────────────┐
│value_id│entity_type_id│attribute_id│store_id│entity_id│value       │
├────────┼──────────────┼────────────┼────────┼─────────┼────────────┤
│1       │10            │96          │0       │1        │English name│
├────────┼──────────────┼────────────┼────────┼─────────┼────────────┤
│2       │10            │96          │1       │1        │Polish name │
├────────┼──────────────┼────────────┼────────┼─────────┼────────────┤
│6       │10            │96          │2       │1        │Polish name │
└────────┴──────────────┴────────────┴────────┴─────────┴────────────┘

What is that? I must make additional operations between those setData? If I comment description part everything works well 🙁

Best Answer

The problem is that the second save uses the last setData called so basically you have the following situation.

  1. on first call to save the data is:
    • name: English name,
  2. on the second call to save the data is:
    • name: Polish name,
  3. on the third call to save the data is:
    • name: Polish name,
    • description: English,
  4. on the forth call to save the data is:
    • name: Polish name,
    • description: Polish,

You could either set the data at the same time:

$product = Mage::getModel('catalog/product');
$product->setStoreId(1)->setData('name', 'English name')->setData('description', 'English')->save();
$product->setStoreId(2)->setData('name', 'Polish name')->setData('description', 'Polish')->save();

Or you could do this by using an array of data:

$product = Mage::getModel('catalog/product');
$product->setStoreId(1)->setData(array('name' => 'English name', 'description'=> 'English'))->save();
$product->setStoreId(2)->setData(array('name' => 'Polish name', 'description' => 'Polish'))->save();

Summary

Basically because there is no load/reset of the object in between the saves the data is always the same as the last save but with only the store id and what ever has been specifically changed by calling set data. The setStoreId does not load the store specific values is simply sets an id.

Related Topic