WebAddict
Total Posts: 6
Joined: 2007-12-20
Chandler, AZ
What I need most is some sort of API provided by Magento to update data externally.My products’ manufacturer/drop shipper provides me with the ability to retrieve price and inventory levels from a SOAP web service. I’d build a simple cron script to retrieve a updated info from the manufacturer, and then update the info in Magento via the API. I realize that I can get around this by updating the database directly, but that may not work right with the caching, multiple stores and other things built into Magento, especially as it evolves in the future. An API would be built onto Magento’s core functionality, and would naturally evolve as Magento does.
I would like to minimally be able to have the following functionality from a Magento provided API:
* Update Product Pricing (cost, retail & our price)
* Update Inventory (stock level & status)
Additionally It would be awesome to be able to have the following API Features as well
* Manage Products (Add/Edit/Delete/Disable)
* Manage Categories (Add/Edit/Delete/Disable)
* Manage Attributes & Attribute Groups (Add/Edit/Delete/Disable)
* Manage Tags (Add/Edit/Delete/Disable)
I’d be able to do so much if I could automate these things instead of having to do them by hand in the Magento interface.
Rich
Signature
I Love Magento!
Posted: January 4 2008
| top
Moshe
Total Posts: 1771
Joined: 2007-08-07
Los Angeles
Hi, regarding the 2 first questions:
* Update Product Pricing (cost, retail & our price)
// initialize magento environment for 'base' storerequire_once MAGE_BASE_DIR . '/app/Mage.php' ; Mage :: app ( 'base' ); // load product data $product = Mage :: getModel ( 'catalog/product' )-> load ( $productId ); // update attributes with new values. 'attribute_name' -> $product->setAttributeName($value) $product -> setPrice ( $price ) -> setCost ( $cost ) -> setMsrp ( $msrp ); // save the product $product -> save ();
* Update Inventory (stock level & status)
// load stock item for specific product$stock = Mage :: getModel ( 'cataloginventory/stock_item' )-> loadByProduct ( $productId ); // update stock values $stock -> setQty ( $newQty ) -> setIsInStock ((bool) $status ); // save stock record $stock -> save ();
For other queries we will have to wait for API documentation
Signature
- I would love to change the world, but they won’t give me the source code -
Posted: January 4 2008
| top
| # 1
WebAddict
Total Posts: 6
Joined: 2007-12-20
Chandler, AZ
Awesome, that is exactly what I need right now. I will look forward to the API maturing. Thank you Moshe.
Signature
I Love Magento!
Posted: January 4 2008
| top
| # 2
Bojan Hrnkas
Total Posts: 22
Joined: 2008-04-16
Could one create a new instance of catalog/product, set the attributes/categories/store_id and save it in order to add new product to a shop?
Something like following:
$newProduct = Mage :: getModel ( 'catalog/product' ) -> setStoreId ( $store_id ) -> setCategoryIds ( $category_ids ) -> setPrice ( $price ) -> setName ( $name ) -> setDescription ( $desc ) ... -> save ();
Thanks!
Bojan Hrnkas
Posted: April 16 2008
| top
| # 3
Bojan Hrnkas
Total Posts: 22
Joined: 2008-04-16
I tried the code I wrote above, and I managed to add a new product that way.
<?php // initialize magento environment for 'default' store require_once 'app/Mage.php' ; Mage :: app ( 'default' ); try { $newProduct = Mage :: getModel ( 'catalog/product' ) -> setStoreId ( 'default' ) -> setCategoryIds ( '4' ) -> setAttributeSetId ( 41 ) -> setPrice ( 15.49 ) -> setSku ( '4you-1' ) -> setName ( '4You (M)' ) -> setDescription ( 'A new T-Shirt line from Fruit of Loom' ) -> save (); echo 'OK Product ID: ' . $newProduct -> getId (); } catch ( Mage_Core_Exception $e ) { echo $e -> getMessage (); } catch ( Exception $e ) { echo $e ; }
Answer was: “OK Product ID: 135”
It would be nice if a expirienced Magento developer or a team member could comment on this, so I can put it in Wiki if it is correct as is.
Further, it would be nice to have a snippet how to get AttributeSetId from name and maybe the same for Categories.
Thanks!
Bojan Hrnkas
Posted: April 16 2008
| top
| # 4
peterw83
Total Posts: 44
Joined: 2007-11-12
Any idea when we will have some basic API documentation to build on?
Posted: April 17 2008
| top
| # 5
Loxam
Total Posts: 60
Joined: 2008-03-17
Can someone help me as to where I use this code, I need to get the updating inventory use in our shop asap!
Posted: June 19 2008
| top
| # 6
orbi
Total Posts: 4
Joined: 2007-11-20
Hi,
we have to create bulk coupons. Is the API somehow able to do this? (And is magento able to handle hundreds or thousands of coupons?)
thanks
orbi
Posted: June 20 2008
| top
| # 7
allstarbooks
Total Posts: 4
Joined: 2008-07-19
Here is the code to get an attribute set’s ID by name:
$attributeSetId = Mage :: getSingleton ( 'catalog/config' )-> getAttributeSetId ( 'catalog_product' , '<some_attribute_set_name>' );
Posted: July 20 2008
| top
| # 8
itzik
Total Posts: 25
Joined: 2008-07-21
Moshe - 04 January 2008 07:00 AM
Hi, regarding the 2 first questions:
* Update Product Pricing (cost, retail & our price)
// initialize magento environment for 'base' storerequire_once MAGE_BASE_DIR . '/app/Mage.php' ; Mage :: app ( 'base' ); // load product data $product = Mage :: getModel ( 'catalog/product' )-> load ( $productId ); // update attributes with new values. 'attribute_name' -> $product->setAttributeName($value) $product -> setPrice ( $price ) -> setCost ( $cost ) -> setMsrp ( $msrp ); // save the product $product -> save ();
* Update Inventory (stock level & status)
// load stock item for specific product$stock = Mage :: getModel ( 'cataloginventory/stock_item' )-> loadByProduct ( $productId ); // update stock values $stock -> setQty ( $newQty ) -> setIsInStock ((bool) $status ); // save stock record $stock -> save ();
For other queries we will have to wait for API documentation
Shalom Moshe.....I tried updating the inventory level, but I get this error:
Fatal error : Uncaught exception 'Zend_Db_Statement_Exception' with message 'SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`liquncom_magento/cataloginventory_stock_item`, CONSTRAINT `FK_CATALOGINVENTORY_STOCK_ITEM_PRODUCT` FOREIGN KEY (`product_id`) REFERENCES `catalog_product_entity` (`entity_id`) ON DELETE CASCA)' in / home / liquncom / public_html / lib / Zend / Db / Statement / Pdo . php : 238 Stack trace : #0 /home/liquncom/public_html/lib/Zend/Db/Statement.php(283): Zend_Db_Statement_Pdo->_execute(Array) #1 /home/liquncom/public_html/lib/Zend/Db/Adapter/Abstract.php(406): Zend_Db_Statement->execute(Array) #2 /home/liquncom/public_html/lib/Zend/Db/Adapter/Pdo/Abstract.php(206): Zend_Db_Adapter_Abstract->query('INSERT INTO `ca...', Array) #3 /home/liquncom/public_html/lib/Zend/Db/Adapter/Abstract.php(484): Zend_Db_Adapter_Pdo_Abstract->query('INSERT INTO `ca...', Array) #4 /home/liquncom/public_html/app/code/core/Mage/Core/Model/Mysql4/Abstract.php(343): Zend_Db_Ada in /home/liquncom/public_html/lib/Zend/Db/Statement/Pdo.php on line 238
What do you think the problem is?
Posted: August 2 2008
| top
| # 9
allstarbooks
Total Posts: 4
Joined: 2008-07-19
make sure the db tables are InnoDB…
itzik
Total Posts: 25
Joined: 2008-07-21
How do I do that?
Thank you very much for your assistance...it’s very much appreciated.
Refugnic
Total Posts: 27
Joined: 2008-07-24
Lichtenfels
To check for the format of your tables, you just need to open PHPMyAdmin (http://yourserver/phpmyadmin ), go to your magento database and look at the formating.