
Magento的tag是点击后,有一个tag关联商品的list页面。客户需要在商品详情页面product view中点击tag使用ajax直接显示商品。
首先我们需要写个小插件,为ajax提供数据
Controller的参考代码如下:
$tagId = $this->getRequest()->getParam('tagId'); $tag = Mage::getModel('tag/tag')->load($tagId); if (!$tag->getId() || !$tag->isAvailableInStore()) { $this->_forward('404'); return; } $page_size = 15; $page = 1; /* $tagModel = Mage::getModel('tag/tag'); $productCollection = $tagModel->getEntityCollection() ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes()) ->addTagFilter($tagId) ->addStoreFilter(Mage::app()->getStore()->getId()) ->addMinimalPrice() ->addUrlRewrite() ->setActiveFilter() ->setPageSize($page_size) ->setCurPage($page); Mage::getSingleton('catalog/product_status')->addSaleableFilterToCollection($productCollection); Mage::getSingleton('catalog/product_visibility')->addVisibleInSiteFilterToCollection($productCollection); */ $productIds = $tag->getRelatedProductIds(); $productCollection = Mage::getModel('catalog/product')->getCollection() ->setStoreId($storeId) ->addAttributeToSelect('*') ->addFieldToFilter('entity_id', $productIds) ->setPageSize($page_size) ->setCurPage($page); $html = ''; foreach ($productCollection as $product) { //to get all product list $html .= '<div class="item">'; //START $p_url = $product->getProductUrl(); $p_name = $product->getName(); $p_img_url = Mage::helper('catalog/image')->init($product, 'small_image')->constrainOnly(true)->keepAspectRatio(true)->keepFrame(false)->resize(200, null); $p_final_price = Mage::helper('core')->currency($product->getFinalPrice(), true, false); $html .='<a class="product-image" href="' . $p_url . '"><img src="' . $p_img_url . '" width="200" /></a>'; $html .='<h2 class="product-name" class="product-name"><a href="' . $p_url . '">' . $p_name . '</a></h2>'; $html .='<div class="price-box"><span class="regular-price"><span class="price">' . $p_final_price . '</span></span></div>'; //END $html.='</div>'; } //var_dump($productCollection); echo $html;
模板文件中的ajax获取Controller返回商品信息的参考代码如下