Kind of a spinoff of this question. I'm trying to extend Mage_Catalog_Block_Product_Widget_Link to make a widget template that displays the product image and an add to cart button along with the product link. Is there any way to get this info without doing Mage::getModel('catalog/product')->load()?
What Have You Tried
The working-but-ugly way: I know I could split the product id off the
id_pathstring supplied by the widget and use it to load the full product, like so:$idPath = explode('/', $this->_getData('id_path')); if (isset($idPath[1])) { $id = $idPath[1]; if ($id) { $product = Mage::getModel('catalog/product')->load($id); /* Use $product to get the needed info */ } }but this means I'm fetching a big giant product object with lots of data I don't need, possibly several times per page. I'm under the impression that this is not desirable. (Am I wrong?)
- Use getAttributeRawValue in the resource model: I have the resource model available in
$this->_entityResourcebut I can't usegetAttributeRawValueto get the add to cart URL since it's not an attribute. I can get the value of the image attribute this way but can't figure out how to use that to actually display the image. All the templates I've looked at use$this->helper('catalog/image')which eventually requires a product object. I'd like to be able to use the cache if possible so just straight-up appending it to /media/product/catalog seems like a bad idea. (Wrong?) - Use the getAddToCartUrl function: This also appears to require a product object as the first argument.
Is there a better way I'm not aware of?
This is on CE 1.9, if that makes a difference.
1 Answer 1
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);
}
-
1Thanks for your answer. I'm trying to understand the first half... Why is it better to get the product ID this way instead of exploding the path_id? Are there situations where that technique won't work?Yumecosmos– Yumecosmos2014年08月13日 18:32:39 +00:00Commented Aug 13, 2014 at 18:32