Magento – Attributes with Website Scope Empty in Frontend When Imported with Store ID

attributesimportscope

All product attributes with website scope are not showing any values in frontend after import. In the backend they do. I have one website, one store group and two store views (languages). In the backend the correct values are shown in both store views with a "[WEBSITE]" behind the textbox. The default values are empty.

To open the product in the backend on global scope and save it again fixes the problem.

The problem occured after some updates and reimporting all products. I'm importing by doing something like this:

$product = Mage::getModel('catalog/product');
$product
  ->setWebsiteIds(array('1'))
  ->setStoreId('1')
  ->setStatus(1)
  ->setSku('123')
  ...
  ->setAttribute('...')
  ->save();

Reindexing doesn't help.

Why are the attribute values not shown before saving via the admin?

UPDATE

I took a look to the DB. Before saving via Admin, I have two (identical) values, one for each store view. After there is a third with store_id = 0 and value = NULL.

I also tried without ->setStoreId('1') … then the insert of a new product works, but the update doesn't work any longer. I looked to the DB again and only the value for store_id = 0 got updated.

Reformulated Question: Is it correct, that I have to handel insert and update differently, or am I missing something … and if yes – why?

(I'm still very much confused with the scopes and in this case trying to brake it down to an explicit szenario.)

Summary

1st TRY: only ->setWebsiteIds(array('1')), here insert works, update doesn't.

Values after insert via script:

store_id    values
0           initial_value
1           initial_value
2           initial_value
... right value on the frontend :-)

Values after update via script:

store_id    values
0           updated_value
1           initial_value
2           initial_value
... old value on the frontend :-(

2nd TRY: ->setWebsiteIds(array('1')) and ->setStoreId('1'), here other way round: update works, insert doesn't (without open and save in the backend)

Values after insert via script:

store_id    values
1           initial_value
2           initial_value
... no value on the frontend :-(

Values after open/save via backend:

store_id    values
0           NULL
1           initial_value
2           initial_value
... right value on the frontend :-)

Values after update via script:

store_id    values
0           NULL
1           updated_value
2           updated_value
... right value on the frontend :-)

Best Answer

The problem was caused from another part of the script which loaded the product again to set the store view specific values. And the problem was to set the store id before loading the product.

My old code:

$product_de = Mage::getModel('catalog/product');
$product_de
    ->setStoreId('1')
    ->load($id)
    ... // set some store view specific data
    ->save();

The fixed code:

$product_de = Mage::getModel('catalog/product');
$product_de
    ->load($id)
    ->setStoreId('1')
    ... // set some store view specific data
    ->save();

Now only one entry get's inserted in the catalog_product_entity_* table – with store_id = 0.

Strange because I've seen it in the other order e.g. here: http://www.webguys.de/magento/produkte-mehrsprachig-importieren/ (german website)

Don't know if this is a bug, or there is a reasonable explanation for this behaviour.