【问题标题】:latest product with add-to-cart in magento在 magento 中添加到购物车的最新产品
【发布时间】:2013-11-22 05:37:39
【问题描述】:

我是 Magento 的新手。我需要在主页显示最新添加的产品。我在2columns-left.phtml中使用以下代码

<div class="main">
<?php echo $this->getChildHtml('breadcrumbs') ?>
    <div class="col-main">
        <?php echo $this->getChildHtml('global_messages') ?>
        <?php echo $this->getChildHtml('content') ?>
        <?php
            $productsCollection = Mage::getModel("catalog/product")->getCollection()
                               ->addAttributeToSort("entity_id","DESC")
                               ->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInSiteIds())
                               ->setPageSize(6)
                               ->setCurPage(1);
            foreach ($productsCollection as $product) {
                $model = Mage::getModel('catalog/product')->load($product->getId());
                echo $model->getName().'<br>'; 
                echo $model->getPrice().'<br>'; 
                echo $model->getImageUrl().'<br>';
                echo "<br><br>";
            } 
        ?>
    </div>
</div>

一切正常。但我无法更正 add-to-cart 选项的代码。

我从addtocart.phtml 找到了带有脚本的流代码,

<?php $_product = $this->getProduct(); ?>
<form action="<?php echo $this->getSubmitUrl($_product) ?>" method="post" id="product_addtocart_form">
<?php $buttonTitle = $this->__('Add to Cart'); ?>
<?php if($_product->isSaleable()): ?>
    <div class="add-to-cart">
        <?php if(!$_product->isGrouped()): ?>
        <label for="qty"><?php echo $this->__('Qty:') ?></label>
        <input type="text" name="qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__('Qty') ?>" class="input-text qty" />
        <?php endif; ?>
        <button type="button" title="<?php echo $buttonTitle ?>" class="button btn-cart" onclick="productAddToCartForm.submit(this)"><span><span><?php echo $buttonTitle ?></span></span></button>
        <?php echo $this->getChildHtml('', true, true) ?>
    </div>
<?php endif; ?>
</form>

但是,如果我点击添加到购物车,就会转到主页。

帮帮我。

【问题讨论】:

  • 无需重定向。如果只是加入购物车就足够了。

标签: magento e-commerce cart php


【解决方案1】:

通过静态块调用主页上的块,内容值为

{{block type="catalog/product_list" name="product_listing" template="catalog/product/latest.phtml"}}

或者你可以直接在 cms home 上调用这个块,粘贴这个

{{block type="catalog/product_list" name="product_listing" template="catalog/product/latest.phtml" }}

在您的 cms 主页内容中。

在文件夹 frontend/theme_folder/default/template/catalog/product 中创建一个文件作为 latest.phtml。

将以下代码粘贴为:

最新产品

<?php
$_productCollection = Mage::getResourceModel('reports/product_collection')
                    ->addAttributeToSelect('*')
                    ->setVisibility(array(2,3,4))                   
                    ->setOrder('created_at', 'desc')
                    ->setPage(1, 4);
?>
 <?php $_iterator = 0; ?>
     <ul class="products-grid">
            <?php  foreach($_productCollection as $_product) : ?>
            <li class="item<?php if( ++$_iterator == sizeof($_productCollection) ): ?> last<?php endif; ?>">
                <?php // Product Image ?>
                       <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135); ?>" width="135" height="135" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a>

                      <div class="product-shop">
                            <div class="f-fix">
                                <h2 class="product-name"><a href="<?php echo $_product->getProductUrl(); ?>"><?php echo $_product->getName(); ?></h2>

                                 <?php echo $this->getPriceHtml($_product, true) ?>
                                <?php if($_product->isSaleable()): ?>
                                    <p><button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button></p>
                                <?php else: ?>
                                    <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                                <?php endif; ?>
                            </div>
                    </div>
              </li>
        <?php endforeach;  ?>
    </ul>

就是这样,您将能够在主页上看到最新的 4 产品。您可以通过增加 ->setPage(1, 4); 来增加产品的数量;到您在主页上所需的产品数量。

【讨论】:

  • 谢谢。但添加到购物车按钮不起作用。它只是进入产品页面。
  • 无需重定向。如果只是加入购物车就足够了。
  • 你可以使用 ajax getIdBySku($p); ?> load($productid); ?> init(); ?> addProduct($my_product, array('qty' => $ar)); ?> save(); ?> setCartWasUpdated(true); ?>
猜你喜欢
  • 2023-03-10
  • 2017-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多