Question

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?

Was it helpful?

Solution

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));
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top