1

Note: This is on Magento EE 1.13.1.0

I had to extend Mage_Catalog_Model_Api2_Product_Rest_Guest_V1::_prepareProductForResponse to make some custom changes to the product feed. But, interestingly, the url being returned from the products has the admin url, and not the frontend store url. For isntance, I make this call to the API:

http://store.example.dev/api/rest/products?store=33&limit=100&page=1&category_id=63

And the tag returned in the feed is this:

http://admin.example.dev/index.php/the-product.html

Instead of:

http://store.example.dev/the-product.html

Near the top of the _prepareProductForResponse method, I can see that it's trying to set the website id on the product object:

$product->setWebsiteId($this->_getStore()->getWebsiteId());

But that doesn't seem to have any effect. I tried setting the store id instead with:

$product->setStoreId($this->_getStore()->getId());

That works to get the correct url, but it also ends up adding the store parameter to the url, even though the system config is set to not display it.

I thought about doing a preg_replace to just remove the store param, but that is hacky, and I don't want to do that.

Anybody have any ideas on how to fix this?

2
  • I got same problem 2 years ago on magento ce 1.6. I thought it's only me who got that problem as I can't find any thread about that at that time. I'll post something when I'm back on my laptop
    – ivantedja
    Jul 25, 2015 at 4:42
  • Isn't it normal in the API that the admin store is loaded (ID 0)? Use store-emulation with the shop you'd like to target for URLs before loading the product models. Once a product model has an URL set internally, changing the store ID won't make a difference. IIRC both, the storeID as well as the URL are kept with the object in memory. Alternative to store-emulation is that you invalidate the internal data of the object, but I would go with store-emulation first inside the API.
    – hakre
    Jul 26, 2015 at 11:29

1 Answer 1

0

As I said previously on the comment, this problem did occur to me long time ago. But somehow I couldn't find anyone posting this problem. I myself think this is Magento's bug.

I did a simple testing on shell script:

class Shell_Derp extends Mage_Shell_Abstract
{

    public function run()
    {
        $productHelper = Mage::helper('catalog/product');

        $product1 = Mage::getModel('catalog/product')->setStoreId(1)->load(1);
        echo $productHelper->getProductUrl($product1->getId()) . "\n";

        $product2 = Mage::getModel('catalog/product')->setStoreId(1)->load(1);
        echo $productHelper->getProductUrl($product2) . "\n";
    }
}

output:

$ ivantedja:shell ivantedja$ php derp.php
http://local.mg.com/derp.php/catalog/product/view/id/1/s/fit-2-pack/
http://local.mg.com/fit-2-pack

if we take a look at Mage::helper('catalog/product'):

// Mage_Catalog_Helper_Product

/**
 * Retrieve product view page url
 *
 * @param   mixed $product
 * @return  string
 */
public function getProductUrl($product)
{
    if ($product instanceof Mage_Catalog_Model_Product) {
        return $product->getProductUrl();
    }
    elseif (is_numeric($product)) {
        return Mage::getModel('catalog/product')->load($product)->getProductUrl();
    }
    return false;
}

There is no way to specify store id on the parameter, so that if we provide product id it will instantiate a new product object (using the incorrect store id). So my solution was by changing $product->getId() with $product which means we provide product object with the correct store id

As I was only using the REST API for guest's role, my solution was to rewrite Mage_Catalog_Model_Api2_Product_Rest_Guest_V1 (you need to rewrite other role if needed -> Admin, Customer) OR you can also define whole class rewrite by copying whole code to app/code/local/Mage/Catalog/Model/Api2/Product/Rest.php (even though you are only modifying one line of code). Choose whichever you want.

class Anything_Catalog_Model_Api2_Product_Rest_Guest_V1 extends Mage_Catalog_Model_Api2_Product_Rest_Guest_V1
{
    protected function _prepareProductForResponse(Mage_Catalog_Model_Product $product)
    {
        parent::_prepareProductForResponse($product);

        $productHelper = Mage::helper('catalog/product');

        /** handling product url, buggy? */
        if ($product->hasData('url')) {
            $product->setData('url', $productHelper->getProductUrl($product));
        }
    }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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