Magento:$ this-> getAddToCartUrl($ product)在我尝试在主页

时间:2015-07-02 12:02:55

标签: php magento

我是Magento的新手,因此我陷入了不同的境地。我找到了一个关于如何在主页上展示畅销产品的指南,实现它并且一切正常,除了$ this-> getAddToCartUrl($ product)。单击"添加到购物车"后,产品不会添加到购物车中。 这是我的代码:

$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>

提前谢谢,我希望我的问题能在这里得到解决。

2 个答案:

答案 0 :(得分:0)

按照以下步骤更改按钮。

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

答案 1 :(得分:0)

我找到了解决方案。问题出在管理员面板上。

我遇到的问题不是代码...... 当我进入管理产品和单个产品时,我看到我想要添加到购物车的产品只有1个数量,所以我已经将它们添加到购物车中,所以我再也不能了。

我更改了数量超过1的数量。它有效。

对不起,谢谢你的时间,一切顺利