Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

[solved - Magento Core API Released] Magento API
 
WebAddict
Jr. Member
 
Avatar
Total Posts:  8
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

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

Hi, regarding the 2 first questions:

* Update Product Pricing (cost, retail & our price)

// initialize magento environment for 'base' store
require_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

 
Magento Community Magento Community
Magento Community
Magento Community
 
WebAddict
Jr. Member
 
Avatar
Total Posts:  8
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.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Bojan Hrnkas
Member
 
Total Posts:  51
Joined:  2008-04-15
 

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

 
Magento Community Magento Community
Magento Community
Magento Community
 
Bojan Hrnkas
Member
 
Total Posts:  51
Joined:  2008-04-15
 

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

 
Magento Community Magento Community
Magento Community
Magento Community
 
peterw83
Member
 
Total Posts:  44
Joined:  2007-11-12
 

Any idea when we will have some basic API documentation to build on?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Loxam
Member
 
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!

 
Magento Community Magento Community
Magento Community
Magento Community
 
orbi
Jr. Member
 
Total Posts:  4
Joined:  2007-11-19
 

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

 
Magento Community Magento Community
Magento Community
Magento Community
 
allstarbooks
Jr. Member
 
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>');
 
Magento Community Magento Community
Magento Community
Magento Community
 
itzik
Member
 
Total Posts:  36
Joined:  2008-07-21
 
Moshe - 04 January 2008 06:00 AM

Hi, regarding the 2 first questions:

* Update Product Pricing (cost, retail & our price)

// initialize magento environment for 'base' store
require_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 errorUncaught 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?

 
Magento Community Magento Community
Magento Community
Magento Community
 
allstarbooks
Jr. Member
 
Total Posts:  4
Joined:  2008-07-19
 

make sure the db tables are InnoDB…

 
Magento Community Magento Community
Magento Community
Magento Community
 
itzik
Member
 
Total Posts:  36
Joined:  2008-07-21
 

How do I do that?

Thank you very much for your assistance...it’s very much appreciated.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Refugnic
Jr. Member
 
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.

 
Magento Community Magento Community
Magento Community
Magento Community
 
fransemail
Jr. Member
 
Total Posts:  9
Joined:  2008-04-28
 

Hi, I’ve the same issue and all tables are InnoDB ones. Did you resolve this problem? May you post this resolution?

 
Magento Community Magento Community
Magento Community
Magento Community
 
bwilhelm
Jr. Member
 
Total Posts:  4
Joined:  2009-09-03
 

Here’s how I did it: http://www.bwilhelm.com/2010/01/05/get-xml-data-into-magento-via-magento-api/

 
Magento Community Magento Community
Magento Community
Magento Community
 
dgiouzelis
Jr. Member
 
Total Posts:  10
Joined:  2008-08-04
 

Hi
do you have any idea, how can i update the group price for a product?
i canot find any documentation around for that. i can only find (and its working perfect) for tier price..

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top