为什么产品网址在magento中重复

时间:2014-04-18 07:10:41

标签: php magento

我正在使用Magento 1.7.0。我想在主页边栏上显示最新订购的产品。除了URL之外,几乎所有内容都已准备就绪并正常工作。

我不知道为什么产品网址会一再重复。产品'图片,它的名称和价格显示正确,只有每个产品的网址重复。

每个产品都有相同的第一个产品网址。

这是代码

        <style>
        .productList { float:left; width:720px; }
        .productList .product { background:#fff; border:1px solid #eedfa6;  padding:3px;   float:left; text-align:center; width: 182px; margin-bottom: 7px; height: 200px; }
        .productList .product .cl { padding:0 0 0 0px; text-align:center;}
        .productList .product .prodDetails {font-weight: bold;font-size: 12px; color: #2a2a28;text-align:center; padding:0 0 0 0px;  }
        .productList .product .prodDetails a{color: #d77932;}
        .productList .product .prodDetails a:hover{color: #3d4d68 ;}
        .productList .product .addCompare { text-align:center;  clear:both; }
        .productList .product .addWishlist {clear:both; text-align:center; }

        }
        </style>
        <?php
        $result = $this->getBestsellerProduct();
        $itemPerRow = ($this->getItemsPerRow()) ? $this->getItemsPerRow() : 2 ;
        //getting product model
        $model = Mage::getModel('catalog/product');
        $products_price = Mage::getStoreConfig('bestseller/general/products_price');  
        $review = Mage::getStoreConfig('bestseller/general/review');  
        ?>
        <div style="width:200px;float:right;">
        <div class="sectionHead" style=""><h2><?php echo $this->getHeader(); ?></h2><div style="height: 25px;border-bottom: 2px solid #eedfa6 /*#DBDBDB;*/"    class="hrline"></div></div>
        <table border="0" cellpadding="0" cellspacing="0" class="productList" style="width:100px;">

            <?php  $i=0;
            for($cnt = 0; $cnt<5; $cnt++){
            $_product = $model->load($result[$cnt]); //getting product object for particular product id

            ?>
            <tr>
            <td class="product">
            <div class="prodimage">
            <!-- code added by Saurabh-->
            <?php echo $_product->getProductUrl() ?>
            <?php if ($_product->getExt_image_url()!=''){$srcImage=$_product->getExt_image_url();} else{$srcImage=$this->helper('catalog/image')->init($_product, 'small_image');} ?>
            <!--saurabh code ended here-->
            <a href="<?php echo $_product->getProductUrl() ; ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>">
            <img src="<?php echo $srcImage; ?>" width="150" height="145" alt="<?php echo $this->htmlEscape($_product->getName()) ?>"/>
            </a>
            </div>
            <div align="center">
            <div class="prodDetails" align="center" style="width:175px;" >

                                                        <?php $_product_name=  $_product->getName(); ?>
                    <?php $_short_product_name = substr($_product_name, 0, 40);  ?>
                                                        <?php  $_dot_product_name=$_short_product_name.'...' ; ?>
            <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $_dot_product_name ;?></a>

            <?php  if($review == 1) 
            {
            $_product = Mage::getModel('catalog/product')->load($_product->getId());
            echo $this->getReviewsSummaryHtml($_product, 'short'); 
            }
            if($products_price == 1) 
            {
            $_product = Mage::getModel('catalog/product')->load($_product->getId());
            echo $this->getPriceHtml($_product, true); 
            } 
            ?>                
            </div>
            <?php $addtocartval = $this->getAddToCart();
            if($addtocartval == 1){
            if($_product->isSaleable()): ?>

            <button class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><?php echo $this->__('Add to Cart') ?></span></button>
            <?php else: ?>
            <span class="out-of-stock"><?php echo $this->__('Out of stock') ?></span>
            <?php endif; }?>
            </div>
            <div class="cl">
            <?php $wishlists = $this->getActive();
            if($wishlists == 1){
            if ($this->helper('wishlist')->isAllow()) : ?>
            <a href="<?php echo $this->getAddToWishlistUrl($_product) ?>" class="addWishlist"><?php echo $this->__('Add to Wishlist') ?></a>
            <?php endif; }?>
            </div>  
            <div class="cl">
            <?php $addtocompare = $this->getAddToCompare();
            if($addtocompare==1){
            if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
            &nbsp;<a href="<?php echo $_compareUrl; ?>" class="addCompare"><?php echo $this->__('Add to Compare') ?></a>
            <?php endif; }?>
            </div>
            </td>

            </tr>
            <?php
            }
            ?>
            </table>
            </div>

1 个答案:

答案 0 :(得分:0)

首先,检查$result[$cnt]same for every time ??

     for($cnt = 0; $cnt<5; $cnt++){
    // change code here
     $_product = Mage::getModel('catalog/product')->load($result[$cnt]);
     $my_product_url = $product->getProductUrl();
    //or
    $my_product_url=Mage::getResourceSingleton('catalog/product')
      ->getAttributeRawValue($result[$cnt], 'url_key', Mage::app()->getStore());
   //or
    $my_product_url=Mage::getUrl()''.$_product->getData('url_key').'.html';