How to Get Canonical Product URL by Cron Process in Magento 1.8

geturlmagento-1.8producturlurl-rewrite

its really strange to get such kind of issue relate to get product correct url

below is my collection to get product information

    $store = Mage::app()->getDefaultStoreView();
    $rootCategoryId = $store->getRootCategoryId();
    // Gets the current store's id
    $storeId = $store->getStoreId();

  $collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*')
                  ->addStoreFilter($storeId)
                   ->addAttributeToFilter(
                    'entity_id',
                        array('lt' => 2500)
                  )
                  ->addAttributeToFilter(
                    'entity_id',
                        array('gt' => 2100)
                  )     
                  ->addAttributeToFilter(
                    'status',
                        array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
                  )->addUrlRewrite($rootCategoryId);

when i will run this process by CLI ( means any php file to call this collection its given my correct url of product

but problem is occurred when this collection is called by any cron process with cron_schedule table

i am getting below url

http://www.anydomain.com/index.php/catalog/product/view/id/2107/s/product-url

by i need only

http://www.anydomain.com/product-url

above result is working fine with any CLI (means with php file to call this collection)

but its not working with Cron process.

would you please help me to get it done.any suggestion would be appreciate.

Best Answer

Got this work to set store id in product collection's product object

Just need to add this like

 foreach ($collection as $product)
 {
         $product->setStoreId($storeId);
 }

so in mode/product/url.php

this will get true

 $rewrite = $this->getUrlRewrite();
        $rewrite->setStoreId($product->getStoreId())
            ->loadByIdPath($idPath);
        if ($rewrite->getId()) {
}

and you can get value from rewrite table.

hope this will sure help you someone which could be face this type of issue.