7
votes

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?
5

5 Answers

14
votes

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
);
2
votes

add this for specific store view

$product->setStoreId($storeId);
1
votes
$store_id = Mage::app()->getStore()->getStoreId();

$product = Mage::getModel('catalog/product')->setStoreId($store_id);
$brandLabel = $product->setData('brand','adidas')->getResource()->saveAttribute($product, 'brand');
1
votes
<?php $StoreId = Mage::app()->getStore()->getId();

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

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));