Export Magento Product with Full Canonical URL

Is there a way to get a list of products with their current canonical url on the 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

+7
command-line-interface php export canonical-link magento
source share
2 answers

By default, magento uses the same code to get the canonical url in Mage_Catalog_Block_Product_View::_prepareLayout() , so the code must be accurate. The only difference is why the code is stored.

It does not work in shell scripts because they run for the admin store (see Mage_Shell_Abstract::__construct() , where Mage::app() initialized). You can use Mage::app()->setCurrentStore('default'); where you need to replace default with your store, and you need to print the correct URLs.

+4
source share

Maybe I donโ€™t understand what you mean by the "canonical url", but if you mean the product URL with its identifier and the key at the end, which is usually the "canonical url" for magento, since it must be unique in one moment, you should just remove getUrl parameters. If you don't need a key, you can still use:

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

I hope this helps, if not, please clarify the result you want.

-2
source share

All Articles