立即注册 登录
我爱免费网 返回首页

aiqiyi的个人空间 http://www.520free.com/?69 [收藏] [复制] [RSS]

日志

Magento如何调用随机产品代码

已有 2317 次阅读2013-11-6 15:30 |个人分类:aiyiqi的博客| Magento, 效果图, 产品, 如何

以下是我修改后的代码,效果图在代码下面:

<!--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">

    <h2>You May Also Like</h2>

              <div class="like-left">

        <ul>

              <?php

              if(sizeof($result) >= 4)

              {

                 $ourneed = array_rand($result,4);

                 foreach($ourneed as $cc)

                     {

                      $thisproduct= Mage::getModel('catalog/product')->load($result[$cc]);

                      ?>

                      <li>

                      <div class="like-image"><a href="<?php echo $thisproduct->getProductUrl(); ?>" title="<?php echo $thisproduct->getName(); ?>" ><img src="<?php echo $this->helper('catalog/image')->init($thisproduct, 'small_image')->resize(150) ?>" width="150" height="150" alt="<?php echo $thisproduct->getName(); ?>" /></div>

             <div class="like-word">

             <div class="like-word-a"><a href="<?php echo $thisproduct->getProductUrl(); ?>" title="<?php echo $thisproduct->getName(); ?>"><?php echo $thisproduct->getName(); ?></div>

             <?php echo $this->getPriceHtml($thisproduct, true, '-new') ?></div>

                     </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(150) ?>" width="150" height="150" alt="<?php echo $thisproduct->getName(); ?>" />

                <p><?php echo $thisproduct->getName(); ?>

                            </li>

                     <?php

                     }

                     }

                     ?>

              </ul>

        </div>

 

              <div class="like-right">

        <ul>

              <?php

              if(sizeof($result) >= 4)

              {

                 $ourneed = array_rand($result,4);

                 foreach($ourneed as $cc)

                     {

                      $thisproduct= Mage::getModel('catalog/product')->load($result[$cc]);

                      ?>

                      <li>

                      <div class="like-image"><a href="<?php echo $thisproduct->getProductUrl(); ?>" title="<?php echo $thisproduct->getName(); ?>" ><img src="<?php echo $this->helper('catalog/image')->init($thisproduct, 'small_image')->resize(150) ?>" width="150" height="150" alt="<?php echo $thisproduct->getName(); ?>" /></div>

             <div class="like-word">

             <div class="like-word-a"><a href="<?php echo $thisproduct->getProductUrl(); ?>" title="<?php echo $thisproduct->getName(); ?>"><?php echo $thisproduct->getName(); ?></div>

             <?php echo $this->getPriceHtml($thisproduct, true, '-new') ?></div>

                     </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(150) ?>" width="150" height="150" alt="<?php echo $thisproduct->getName(); ?>" />

                <p><?php echo $thisproduct->getName(); ?>

                            </li>

                     <?php

                     }

                     }

                     ?>

              </ul>

        </div>

       </div>

<!--for show other product-->

 

 

P.S

20120426修订版,修复了推荐产品价格与当前产品价格一样的BUG,主要将原来的语句

<?php echo $this->getPriceHtml($_product, true, '-new') ?>

修改为以下语句

<?php echo $this->getPriceHtml($thisproduct, true, '-new') ?>

 

 

相应的CSS样式:

/************************详细页推荐产品*******************/

.box-others-also-like { height:670px; }

.like-left { float:left; width:49%; }

.like-right { float:right; width:49%; }

.like-left li, .like-right li { height:150px; margin-bottom:10px; border:1px #bbbbbb solid; }

.like-image { float:left; width:150px; }

.like-word { float:right; width:150px; margin:0 10px; }

.like-word-a { font-size:14px; margin-top:5px; height:80px; }

 


漂亮

酷毙

雷人

鲜花

鸡蛋

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 立即注册
验证问答 换一个 验证码 换一个

手机版|网站地图|Sitemap|我爱免费网

GMT+8, 2020-4-2 07:26 , Processed in 0.029585 second(s), 18 queries .

找客服

回顶部