1

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 :(

1 Answer 1

2

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.

3
  • Hell yeach!!! It's alive!!
    – kuba_ceg
    Commented Jun 24, 2014 at 14:23
  • i cannot under standard what is different can you please explain
    – Amit Bera
    Commented Jun 24, 2014 at 19:03
  • @AmitBera see my summary Commented Jun 25, 2014 at 6:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.