Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento product export with full canonical url

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

like image 755
Kevin Avatar asked Jun 30 '14 12:06

Kevin


1 Answers

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.

like image 108
Simon H Avatar answered Oct 15 '22 07:10

Simon H