magento产品详细页随机显示产品

magento产品详细页随机显示产品
magento产品详细页随机显示产品

如上图的右边一列产品,就是在我的模板中随机显示的产品。

magento有cross sell和up sell还有相关产品,但是都是需要手动设置的。对于我这样的懒人,懒得设置,并且我的产品相关性没有这么强。所以在产品详细页随机显示一下产品同分类下的其它产品就行了。我google了一把,找到了些代码,做了简单的修改,在magento1.4.1.0中运行没有什么问题。贴出来和大家共享下,还是那句话,css自己改改就可以用了。

如下代码可以放在产品详细页的任何位置,如view.phtml或者media.phtml中。改一改就可以改成随机显示某一个分类下的产品,显示的数量,图片的大小在本代码中都是很容易改的。

<!--for show other product-->
<?php $categories = $_product->getCategoryIds(); ?>
	<?php
		$result = array();
		foreach($categories as $cat_id) {
			$category = Mage::getModel('catalog/category');
			$category->load($cat_id);
			$collection = $category->getProductCollection();
			foreach ($collection as $product) {
				$result[] = $product->getId();
			}

		}
	?>
	<div class="box-others-also-like">
		<ul>
		<?php
		if(sizeof($result) >= 5)
		{
		   $ourneed = array_rand($result,5);
		   foreach($ourneed as $cc)
			{
			 $thisproduct= Mage::getModel('catalog/product')->load($result[$cc]);
			 ?>
			 <li>
			<a href="<?php echo $thisproduct->getProductUrl(); ?>" title="<?php echo $thisproduct->getName(); ?>" ><img src="<?php echo $this->helper('catalog/image')->init($thisproduct, 'small_image')->resize(200) ?>" width="200" height="200" alt="<?php echo $thisproduct->getName(); ?>" /></a>
			</li>
			<?php } ?>
		<?php
		}else
		{
		   foreach($result as $cc)
			{
			 $thisproduct= Mage::getModel('catalog/product')->load($cc);
			 ?>

				<li>
				<a href="<?php echo $thisproduct->getProductUrl(); ?>" title="<?php echo $thisproduct->getName(); ?>" ><img src="<?php echo $this->helper('catalog/image')->init($thisproduct, 'small_image')->resize(200) ?>" width="200" height="200" alt="<?php echo $thisproduct->getName(); ?>" /></a>
				</li>
			<?php
			}
			}
			?>
		</ul>
	</div>
	<!--for show other product-->

代码也没有好好整理,我懒。想弄的好看点自己格式化下代码,下了个格式化代码的工具,php可以格式化,但是咱magento的php是包含html的,汗,没法用。谁有好的格式化咱magento的phtml代码的工具介绍给我。谢谢

转载表明出处:hellokeykey.com

《magento产品详细页随机显示产品》有32个想法

  1. 请教下 代码中cat_id 是自动获取产品所在分类的id么?如果是自动获取到的话 就比较完美了

    另外有个问题,首页希望调一类产品进行展示(通常是几十个吧),分类及产品已经在后台设置ok了 新建了list-home.phtml来单独实现首页调用及css控制,通常会删除toolbar
    但是在前台显示的时候会受到 分类页toolbar工具条的影响,就是:如果在后台设置每页显示9个产品的话,首页没办法正常显示出所有的产品,数量默认变成9个,浏览之后还会受到分类页调整的影响。
    Google了好久都没找到解决方案。不知道你有没有碰到过类似的问题,有什么好的解决方案和建议么? 谢谢

    1. 直接自己用代码调用你的分类下的产品,就想我这篇文章中的代码。这篇文章中的代码是自动获取上一级分类的。你想不受toolbar的影响,就用这篇文章中的代码改个吧。不要用list.phtml的代码了。

  2. 参考你的代码尝试了下
    将list.phtml文件中的
    $_productCollection=$this->getLoadedProductCollection();
    改成
    //$cat_id 暂时指定,还没有研究传参的 方式
    $cat_id = 20;
    $category = Mage::getModel(‘catalog/category’);
    $category->load($cat_id);
    $_productCollection = $category->getProductCollection();
    这样是可以去到整个分类的产品,但是读不出正常的 产品参数,比如 name price special_price in stock等
    汗…. any ideas ?

  3. ok 搞定 还是参考你上面的代码
    在foreach 取产品参数的时候把默认的
    改成
    load($_productid); ?>
    就ok了 🙂
    3q

  4. 汗 怎么分享的代码没了。。。。再发一遍吧 主要修改两部分
    1
    $_productCollection=$this->getLoadedProductCollection();
    $_helper = $this->helper(‘catalog/output’);
    改为 $result = array();
    $_productCollection=$this->getLoadedProductCollection();
    $cat_id = 20;
    $category = Mage::getModel(‘catalog/category’);
    $category->load($cat_id);
    $_productCollection = $category->getProductCollection();
    $_helper = $this->helper(‘catalog/output’);
    foreach ($_productCollection as $product) {
    $result[] = $product->getId();
    }

    2
    foreach ($_productCollection as $_product):
    改为
    foreach ($result as $_productid):
    $_product = Mage::getModel(‘catalog/product’)->load($_productid);
    希望对浏览这篇文章的人有所帮助

  5. 使用你的代码,可以随机显示,但是出现一个问题就是,同一产品会出现在一个页面上。

  6. 我擅自对key中的部分代码,使取出的随机产品不会重复。(别骂我啊,如果有误请指正)

    getCategoryIds(); ?>
    load($cat_id);
    $collection = $category->getProductCollection();
    foreach ($collection as $product) {
    $results[] = $product->getId(); //天下修改 mark
    $result = array_unique($results); //天下修改 mark
    }

    }
    ?>

  7. getCategoryIds(); ?>
    load($cat_id);
    $collection = $category->getProductCollection();
    foreach ($collection as $product) {
    $results[] = $product->getId();
    $result = array_unique($results);
    }
    }
    ?>为什么发上来会不全

  8. $result = array();
    $results = array(); //天下修改 mark
    foreach($categories as $cat_id) {
    $category = Mage::getModel(‘catalog/category’);
    $category->load($cat_id);
    $collection = $category->getProductCollection();
    foreach ($collection as $product) {
    $results[] = $product->getId(); //天下修改 mark
    $result = array_unique($results);//天下修改 mark
    } 最后发一次,不全不怪我。

  9. key, 你好!很感谢你分享的精神,在安装完这个随机产品的模块后,这些随机产品的URL发生了变化:
    例如:http://www.xxxx.com/alex-queen-geom-leather.html
    变成了:http://www.xxxx.com/catalog/product/view/id/1291/s/alex-queen-geom-leather/category/39/
    请问有什么方法可以解决这个问题呢?

    1. 这个不应该的。在magento后台有个将分类url加入到产品url的选项,将那个选项选‘否’,试下

  10. 郁闷…我后台的设置如上面的例子一样,URL里是没有分类名称的,不知是不是版本的问题.

  11. 再补充一点,如果直接在首页点某一产品到产品页,就不会发生上面的问题,如果是从某一分类进去的,就有问题了。而且我试了几个不同的插件,都会发生这种问题,鸣鸣。。

  12. 使用代码后,打开产品也变的很慢。没有使用代码的时候差不多2s不到,使用之后要20s左右了。大家有碰到这样的现象么?

  13. 用看PHP的程序不就得了?我一直用的是EDITPLUS,加一个类型PHTML,就可以把所有PHTML的文件自动转化成EDITPLUS文件。双击就可以打开直接编译了。而且我还一直用它来看CSV的BUG的。。。

  14. URL地址变了,更奇怪的是 :我得出的产品不是这个分类的。
    还有没有好点的办法 得到随机的相关产品,
    现在用1.5 那个添加related的插件用不了了,郁闷

    1. 1.5 那个添加related是自带的,用不了了是模板的问题吧,切换到default模板看下是否在。此文太早了,仅供参考。

      1. 同样的问题, 产品的Url有的重写了,有的没有,期待 群主 给予1.5完美解决,添加related自带的插件,无法显示,不是模板问题,设置了default的还是不可以

        1. url这个要是原先有后台的修改,那么此处要查下输出url的函数是否正确。那个插件是不是不兼容你当前版本?什么插件,未接触过

          1. 在后台已经设置了不加分类到产品url了
            产品页面正常,http://www.xxxx.com/alex-queen-geom-leather.html
            但加这段代码的链接变成了:http://www.xxxx.com/catalog/product/view/id/1291/s/alex-queen-geom-leather/category/39/
            请博主给解决,谢谢, 看到楼上也有同样的情况

  15. url出现目录的问题这样解决: $url = basename($_product->getProductUrl());

    搞了一下午,去官方论坛看到思路了

    1. 这样设置之后目录是消失了 ,但是调用产品的url全部变为一样的了,求解!!!

  16. getUrlpath(); ?> 来替换getProductUrl(); ?>

    根本原因是因为这样调取的时候,如果这个产品不是同分类下的产品,就会出问题

    参考老外的资料 http://stackoverflow.com/questions/11723959/magento-getproducturl-is-not-returning-the-right-url-random

    因为如果不是同分类,magento就会自动这样取网址

    f it found match value in core_url_rewrite, it will generate ‘the correct url’ else it will concat the product_id + url key + category_id

    $routePath = ‘catalog/product/view’;
    $routeParams[‘id’] = $product->getId();
    $routeParams[‘s’] = $product->getUrlKey();
    if ($categoryId) {
    $routeParams[‘category’] = $categoryId;
    }

评论已关闭。