Magento product export with full canonical url
Asked Answered
F

2

8

Is there a way to get a list of products with their current canonical url on command line?

class Mage_Shell_UrlTest extends Mage_Shell_Abstract
{

public function run()
{
    $productCollection = Mage::getResourceModel('catalog/product_collection')
            ->addStoreFilter()
            ->addUrlRewrite()
            ->addAttributeToSelect('*')
            ->setPageSize(10) // just for testing
            ->addFieldToFilter('visibility',Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
            ->addAttributeToFilter('status', array(
                'eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED
            ));

    Mage::getSingleton('cataloginventory/stock')
            ->addInStockFilterToCollection($productCollection);

    foreach ($productCollection as $product) {

        $url = $product->getUrlModel()->getUrl($product, array('_ignore_category' => true));

        echo PHP_EOL . $url . PHP_EOL; // debug output
    }
}
}

$shell = new Mage_Shell_UrlTest();
$shell->run();

I run it with php -f magento/shell/urlTest.php and this gives me something like this:

http://www.domain.com/urlTest.php/catalog/product/view/_ignore_category/1/id/307/s/any_valid_product_url_key

Flatling answered 30/6, 2014 at 12:28 Comment(1)
Magento version 1.7.0.2Flatling
G
4

By default magento uses the same code to get the canonical url in Mage_Catalog_Block_Product_View::_prepareLayout() so the code should be fine. The only difference is for which store the code is executed.

It doesn't work in shell scripts because they are executed for the admin store (see Mage_Shell_Abstract::__construct() where Mage::app() is initialized). You could use Mage::app()->setCurrentStore('default'); where you need to replace default by your store and the right urls should be printed.

Gujral answered 2/7, 2014 at 18:11 Comment(1)
this makes absolutely senseFlatling
I
-2

I maybe do not understand properly what you mean by "canonical url" but if you mean the url of the product with its ID and the key at the end that is normally the "canonical url" for magento as it is supposed to be unique at a moment, you should just take away the params of the getUrl. If you do not want the key, you can still use :

$url = substr($url, 0, strrpos('/s/')); 

I hope it helps, if not, please precise the result you want.

Indore answered 2/7, 2014 at 13:41 Comment(3)
sorry, you should be familar with magento to answer this question. the canonical link is the unique uri to a product without any category-informations or something else. in addition magento has a table called core_rewrite_url to resolve renamed product url keysFlatling
so, please read again my answer and you should get canonical url in the magento definition.Indore
Sorry, this answer is almost dangerous. Please see the accepted answer which is the proper way to do it. Actually I can't see how this can work as a hack anyway.Stiffler

© 2022 - 2024 — McMap. All rights reserved.