Jason
Jason

Reputation: 1542

Access Products/Category/Attribute Info from php with Magento API

Need to be able to pull Magento products into an external template. Need to be able to get all products data (description, title, attributes, categories, image, etc).

And need to be able to filter by category, attribute and also search on name.

These calls will be made from the same server that the Magento install is on. What's the best way to do this?

Will be using php on both linux & windows (2 separate sites).


Have tried using the Magento API & Soap to access from php but haven't been able to get that to work yet. All I get is this error every time.

Fatal error: Uncaught SoapFault exception: [WSDL] SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://mymagento.com/cart/index.php/api/?wsdl' : Extra content at the end of the document in.....

Upvotes: 1

Views: 4107

Answers (3)

Jason
Jason

Reputation: 1542

Here's the basic code we used to get the products. Meshed the code from Laizer's answer with some examples we found on message boards. Worked very well for us.

Note we are filtering by Category ID 11 in the example below.

require_once("app/Mage.php");
Mage::app();


$category = new Mage_Catalog_Model_Category();
$category->getAllChildren(11);

$products = Mage::getModel('catalog/product')->getCollection();
        $products->addAttributeToFilter('status', 1);//enabled
        $products->addCategoryFilter($category);
        $products->addAttributeToSelect('*');
        $prodIds=$products->getAllIds();


        $product = Mage::getModel('catalog/product');
        $count=1;
        foreach($prodIds as $productId)
        {


            echo "$count <br>";
            $product->load($productId);

            $product_data = array();
            $product_data['sku']=$product->getSku();
            $product_data['title']=$product->getName();
            $product_data['description']=$product->getDescription();
            $product_data['link']=$product->getProductUrl();
            $product_data['image_link']=Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'catalog/product'.$product->getImage();
            $product_data['price']=$product->getPrice();
            $product_data['brand']=$product->getResource()->getAttribute('manufacturer')->getFrontend()->getValue($product);
            $product_data['product_type']='';

            //get the product categories
                    foreach($product->getCategoryIds() as $_categoryId){
            $category = Mage::getModel('catalog/category')->load($_categoryId);
            $product_data['product_type'].=$category->getName().', ';
            }
            $product_data['product_type']=rtrim($product_data['product_type'],', ');





echo "<pre>";
            var_dump($product_data);

            //echo 'Loop end: '.memory_get_usage(false).'<br>';
            //flush();
            $count++;
        }

Upvotes: 1

Laizer
Laizer

Reputation: 6140

The easiest route to go is to load up the Magento environment in your PHP script, and use the native Magento calls.

You can load up the environment with the following code:

require_once("path_to_magento/app/Mage.php");
Mage::app();

After that, you can use all of the native methods, just like you would in a Magento .php or .phtml file. For example, to get a product collection and filter it to only active products:

$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToFilter('status', 1); 

Reference for the Magento code base is at: http://docs.magentocommerce.com/

Upvotes: 2

user248810
user248810

Reputation: 536

You can use for example the Mage_CatalogInventory_Model_Stock_Item_Api Class.

Upvotes: 0

Related Questions