I'm trying to integrate some functionality of Magento into my custom CMS to make it easier for my clients to update some of 开发者_JAVA技巧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?
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();
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.
精彩评论