duanfangbunao36970 2015-07-02 12:02
浏览 52

Magento:$ this-> getAddToCartUrl($ product)当我试图在主页上展示最畅销的产品时无法正常工作

I am new to Magento, so I am stucked in a different situation for me. I found a guide on how to display best selling products on homepage, implemented it and everything is working fine, except the $this->getAddToCartUrl($product). The product are not added to the cart after clicking on "Add to cart". Here is my code:

$totalPerPage = ($this->show_total) ? $this->show_total : 4;
$counter = 1;
$visibility = array(
                      Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
                      Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
                  );

$storeId = Mage::app()->getStore()->getId();
$productCollection = Mage::getResourceModel('reports/product_collection')
                              ->addAttributeToSelect('*')
                              ->addOrderedQty()
                              ->addAttributeToFilter('visibility', $visibility)
                              ->setOrder('ordered_qty', 'desc');

<div class="main_content">
    <div class="container">
        <div class="products_list">
            <div class="best_deals">
                <div class="recommended_products best_deals_products">
                    <ul>
            <?php foreach($productCollection as $product): ?>

            <?php
                    $categories = null;
                    foreach (explode(",", $product->category_ids) as $catId){

                      //Mage_Catalog_Model_Category
                      $cat = Mage::getModel('catalog/category')
                                    ->setStoreId(Mage::app()->getStore()->getId())
                                    ->load($catId);
                      $catName = $cat->getName();

                      $catLink = $cat->getUrlPath();
                      $categories .= '<a href="'.$catLink.'" title="'.$catName.'">'.$catName.'</a>&nbsp;&nbsp;';
                    }

            ?>

            <?php if($counter <= $totalPerPage): ?>

            <?php $productUrl =  $product->getProductUrl() ?> 
                        <li>
            <div class="img_bottle">
            <a href="<?php echo $productUrl ?>" title="View <?php echo $product->name ?>">
            <img src="<?php echo $this->helper('catalog/image')->init($product, 'image')->resize(107, 339); ?>" alt="Product image"  />
            </a>
            </div>
            <div class="product_desc">            
            <span><?php echo $product->name ?></span>            
            <span>Our price: $<?php echo number_format($product->getPrice(), 2); ?></span>
            <?php if($product['country_of_manufacture']): ?>
            <span>Country: <i><?php echo strtoupper($product->getAttributeText('country_of_manufacture')); ?></i></span>
            <?php endif; ?>
            <?php if($product['region']): ?>
            <span>Region: <i><?php echo strtoupper($product['region']); ?></i></span>
            <?php endif; ?>                            
                <?php if($product->isSaleable()): ?>
                    <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart buy_now_button" onclick="setLocation('<?php echo $this->getAddToCartUrl($product) ?>')">Add to Cart</button>
                <?php else: ?>
                    <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                <?php endif; ?>
            </div>
                            </li>
            <?php endif; $counter++; ?>

            <?php endforeach; ?>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</div>

Thank you in advance, I hope that my problem will be solved here.

  • 写回答

2条回答 默认 最新

  • duanhui1869 2015-07-02 13:16
    关注

    Change your button with following.

    <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart buy_now_button" onclick="setLocation('<?php echo $this->helper('checkout/cart')->getAddUrl($product)?>')">Add to Cart</button>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示