在magento中,URL,使用magento的方法的magento经常变动,做seo需要稳定的重写后URL譬如:
http://192.168.1.145:81/silverjewellerystores.com/jp/catalog/product/view/id/115/category/882/
我们希望是重写后的URL
http://192.168.1.145:81/silverjewellerystores.com/lovely-heart-style-925-silver-woman-bracelet-b001-283
可以通过下面的方法:
$resource = Mage::getSingleton('core/resource');
$read = $resource->getConnection('core_read');
$productTable = (string)Mage::getConfig()->getTablePrefix().'catalog_product_entity_varchar';
$store_idd = Mage::app()->getStore()->getId();
foreach($ids as $id){
$product_s = Mage::getModel('catalog/product')->load($id);
$select = $read->select()
->from(array('cp'=>$productTable))
->where('cp.entity_id=?', $id)
->where('cp.attribute_id=?',86)
->where('store_id=?',$store_idd)
->limit(3) ;
// echo $select;
$rows = $read->fetchAll($select);
foreach($rows as $row){
$product_url = Mage::getBaseUrl('web').$row['value'];
}
// $product_url = Mage::getBaseUrl('web').$rows[0]['value'];
$product_s->setProductUrls($product_url);
$_producttt[]= $product_s;
}
$tt = count($_producttt);
if($tt<10){
$ss = $tt;
}else{
$ss = 10;
}
$rand=array_rand($_producttt,$ss);
foreach ($rand as $v) {
$_products[]=$_producttt[$v];
}
原理大致为从catalog_product_entity_varchar表中取出URL_key,不同的magento安装后,好像URL_KEY对应的attribute_id貌似不一样,在我的系统里面是86,进而把ID对应的urlkey遍历出来,通过字符串拼接$product_url = Mage::getBaseUrl('web').$row['value'];得到重写后的URL,当然也和重写规则设置对应!!!!