In your collection class (/Some/Module/Model/Mysql4 (or Resource)/YourModel/Collection.php
) add this method:
public function addProductData()
{
/** add particular attribute code to this array */
$productAttributes = array('name', 'price', 'url_key');
foreach ($productAttributes as $attributeCode) {
$alias = $attributeCode . '_table';
$attribute = Mage::getSingleton('eav/config')
->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeCode);
/** Adding eav attribute value */
$this->getSelect()->join(
array($alias => $attribute->getBackendTable()),
"main_table.product_id = $alias.entity_id AND $alias.attribute_id={$attribute->getId()}",
array($attributeCode => 'value')
);
$this->_map['fields'][$attributeCode] = 'value';
}
/** adding catalog_product_entity table fields */
$this->join(
'catalog/product',
'product_id=`catalog/product`.entity_id',
array('sku' => 'sku', 'type_id' => 'type_id')
);
$this->_map['fields']['sku'] = 'sku';
$this->_map['fields']['type_id'] = 'type_id';
return $this;
}
In your grid block use this function:
protected function _prepareCollection()
{
$collection = Mage::getModel('some/yourmodel')
->getCollection()->addProductData();
$this->setCollection($collection);
return parent::_prepareCollection();
}
Case1:
Rewrite the class Mage_Catalog_Block_Widget_Link and on the rewite class add new funtoin getProductId()
on
public function getProductId()
{
if($this->hasStoreId()) {
$store = Mage::app()->getStore($this->getStoreId());
} else {
$store = Mage::app()->getStore();
}
if ($this->getData('id_path')) {
/* @var $urlRewriteResource Mage_Core_Model_Mysql4_Url_Rewrite */
$urlRewriteResource = Mage::getResourceSingleton('core/url_rewrite');
$pID = $urlRewriteResource->getProductPathByIdPath($this->getData('id_path'), $store);
return $pID;
}
return null;
}
Case2:
Rewrite the class Mage_Core_Model_Resource_Url_Rewrite
and on the rewrite class add new funtoin getProductPathByIdPath()
,which will get product
public function getProductPathByIdPath($idPath, $store)
{
if ($store instanceof Mage_Core_Model_Store) {
$storeId = (int)$store->getId();
} else {
$storeId = (int)$store;
}
$select = $this->_getReadAdapter()->select();
/** @var $select Varien_Db_Select */
$select->from(array('main_table' => $this->getMainTable()), 'product_id')
->where('main_table.store_id = :store_id')
->where('main_table.id_path = :id_path')
->where('main_table.product_id IS NOT NULL)
->limit(1);
$bind = array(
'store_id' => $storeId,
'id_path' => $idPath
);
return $this->_getReadAdapter()->fetchOne($select, $bind);
}
this function get Product Id
Case3:
Rewrite the class Mage_Checkout_Helper_Car
and on the rewrite class add new funtoin getmyAddUrl
().By using this you can get cart url
public function getmyAddUrl($productId, $additional = array())
{
$continueUrl = Mage::helper('core')->urlEncode($this->getCurrentUrl());
$urlParamName = Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED;
$routeParams = array(
$urlParamName => $continueUrl,
'product' => $productId
);
if (!empty($additional)) {
$routeParams = array_merge($routeParams, $additional);
}
return $this->_getUrl('checkout/cart/add', $routeParams);
}
Best Answer
Your question has 2 questions: 1. how to regenerate the url keys in Magento 2. how to regenerate the meta data in Magento
To resolve 1: you need to first clear the url keys and then save the attributes and this needs to be done in a batch script
Subset of the code needed:
after this is executed for all your skus, you need to generate url rewrite although the cron will do this for you
I have a full code repository showing how to do the url keys and url rewrite at: https://bitbucket.org/magstaging/urlrewrite