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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With