views:

48

answers:

2

I'm trying to integrate some functionality of Magento into my custom CMS to make it easier for my clients to update some of their products.

I already have classes written to retrieve all the data information I need, but I'm trying to figure out how to save changes to a product in the same fashion (IE, attributes such as color, size, packaging). Is this possible to do through mage?

Right now I essentially construct a class like below, then have various functions to filter products, sessions, and generate thumbnails... but I can't seem to find anything on editing a product.

Mage::app();
$this->model = Mage::getModel('catalog/product');

Has anyone else tried this before?

+3  A: 

You'll want to first load a specific product. You can do that by using the load() method and passing in a product id:

$this->model = Mage::getModel('catalog/product')->load(1111);

You can then set (modify) your product data like this:

$this->model->setName('New Product Name');
$this->model->setPrice(99.99);
$this->model->setShortDescription('New Short Description');

Then simply run the save() method to save the product:

$this->model->save();
Prattski
Is it possible to edit any of attributes on the product? IE, I'm selling bottles of wine and would like to change a few custom attributes such as year, ABV, or type (red/white)?
LinuxGnut
Yes it is possible to change any attribute in this way. A single exception might be images for the product, as they are a little more convoluted.
Joseph Mastey
After I've loaded a product, I can retrieve an attribute I need to edit such as "manufacturer" by doing $this->model->getResource()->getAttribute('manufacturer')->getFrontend()->getValue($product). How do I then set the value after retrieving it?
LinuxGnut
A: 

So after much hassle, I pieced together some code that works:

function setAttribute($pid,$options)
{
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$this->model = Mage::getModel('catalog/product')->load($pid);
foreach ($options as $k=>$v)
{
$this->model->setData($k,$v);
} $this->model->save();
}

You pass a product ID and an array of the data you'd like to update for said product... something like below:

$options = array('labels'=>"No",
'sizee_us_gal'=>1.98,
'size_l'=>7.5,
'makes_us_gal'=>7
'makes_l'=>23,
'timeframe_weeks'=>4,
'composition'=>"grapes",
'packed'=>"case of 2"
);

Hope this helps everyone.

LinuxGnut