2

I'm using Magento API with SOAP, and what I'm trying to achieve is to show the products from my page in an external webpage. I'm showing ID, name, description, image and a link to the page. Everything works fine, except for one thing: The links to the products page are all the same (they all redirect to the first product)

This is my code so far:

<?php
$proxy = new SoapClient('http://127.0.0.1/magento/api/v2_soap/?wsdl'); // TODO : change url
$sessionId = $proxy->login('testUser', 'password'); // TODO : change login and pwd if necessary
$pageURL="categorytree.php/";
$result = $proxy->catalogProductList($sessionId);
//var_dump($result);

echo "<br><br>";


require_once('/magento/app/Mage.php'); //Path to Magento
umask(0);
Mage::app();

$productModel = Mage::getModel('catalog/product');
//var_dump($productModel->getProductUrl);

foreach($result as $product)
{
    echo "ID: $product->product_id <br>";
    $productID = $product->product_id;
    echo "Name: $product->name <br>";
    $p = $productModel->load($productID);
    $description = $p->getDescription();
    echo "Description: $description <br>";
    $imageUrl = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'catalog/product' . $p->getImage();
    $product_collection_url = $p->getProductUrl();
    $product_collection_url = str_replace($pageURL,"",$product_collection_url);
    echo "Image: <a href='$product_collection_url'><img src='$imageUrl'></a><br>";
    echo "<a href='$product_collection_url'>link to page</a><br>";
    echo "<br>";
}
echo "<br><br>";
?>

First I thought it had something to do with the for loop, but each product id, name, description is correct, $product_collection_url is the one causing trouble.

It looks like $p->getProductUrl only works the first time?

2

First, consider not using the API as you are already bootstrapping Magento Core with the require_once('/magento/app/Mage.php'); //Path to Magento

Also you may not need these two lines as well:

umask(0);
Mage::app();

The source of your problem is most likely the collection does not contain the categories:


The problem with this is that products can appear in multiple categories.

Therefore, at some level, regardless of which way you approach it, you have to involve a category somewhere.

So, the cleanest way of doing this is to use the addurlRewrite method and pass the category id. Magento will check for rewrites matching the category id and product id - if a rewrite is found, you will get the nice urls you are looking for on the getProductUrl call. As an example, using a category id of 10:

$collection = Mage::getResourceModel('catalog/product_collection')
                ->addUrlRewrite(10);

If you knew that each product would only appear in a single category, then you could make this a little more flexible by using the following to get the first category id:

Therefore a complete example using this method would be:

$categoryId = array_shift($_product->getCategoryIds());
$collection = Mage::getResourceModel('catalog/product_collection')
                ->addUrlRewrite($categoryId);

Taken from:

Also related question:

  • Well, at first I used the API to fetch information, but as I saw it was incomplete, I added the Mage core. As I see now, it is more complete than the API. If I take out those two lines, I get the next fatal error: Fatal error: Call to a member function getModelInstance() on a non-object in C:\xampp\htdocs\magento\app\Mage.php on line 463 – David Zhou Jul 11 '14 at 16:53
  • The problem with using both is, I expect to use the API from another website, where I do not have access to the Core, so I would need to retrieve all information just with the API. – David Zhou Jul 11 '14 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.