Hubert Perron
Hubert Perron

Reputation: 3022

How to save an attribute value for a specific store view?

I'm trying to create a product programmatically in Magento 1.8 and then set some attribute values to it. So far everyting is working, the attributes are being saved correctly with the product under the "default" scope.

The problem is that my store has two different "store view", one in English and one in French. I can't figure how to set the "scope" or "store view" for the data of a specific attribute.

How can I tell Magento to save an attribute value for a specific scope?

Here's a code sample using the "short description" attribute:

 $product = new Mage_Catalog_Model_Product();
 $product->setSku($sku);
 $product->setAttributeSetId($attributeSetId);
 $product->setTypeId($typeId);
 $product->setName($sku);
 $product->setWebsiteIDs(array($websiteId));
 $product->setShortDescription('Short description in english');
 $product->setShortDescription('Short description in french'); // Scope change here?

Upvotes: 7

Views: 14095

Answers (5)

Marius
Marius

Reputation: 15216

After you have created the product it should have an id.
Here is a fast way to update the product name and short description for a specific store view without calling the resource consuming save method.
Let's assume that the product id is 10 and the store view id is 2.
Run this:

$productId = 10;
$storeId = 2;
$newName = 'Nom de produit';
$newShortDescription = 'description de produit';
Mage::getSingleton('catalog/product_action')->updateAttributes(
    array($productId),
    array('name'=>$newName, 'short_description' => $newShortDescription),
    $storeId
);

Upvotes: 14

user3602718
user3602718

Reputation:

for default store view

$product = new Mage_Catalog_Model_Product();
 $product->setSku($sku);
 $product->setAttributeSetId($attributeSetId);
 $product->setTypeId($typeId);
 $product->setName($sku);
 $product->setWebsiteIDs(array($websiteId));
 $product->setShortDescription('Short description in english');
 $product->setStoreId(array(0));

Upvotes: 1

Ravi Chomal
Ravi Chomal

Reputation: 459

<?php $StoreId = Mage::app()->getStore()->getId();

$product = Mage::getModel('catalog/product')->setStoreId($StoreId);
$brandLabel = $product->setData('brand','adidas')->getResource()->saveAttribute($product, 'brand'); ?>

Upvotes: 1

Kingshuk Deb
Kingshuk Deb

Reputation: 1720

$store_id = Mage::app()->getStore()->getStoreId();

$product = Mage::getModel('catalog/product')->setStoreId($store_id);
$brandLabel = $product->setData('brand','adidas')->getResource()->saveAttribute($product, 'brand');

Upvotes: 1

Ansyori
Ansyori

Reputation: 2835

add this for specific store view

$product->setStoreId($storeId);

Upvotes: 2

Related Questions