0

我有一个相对较小的问题,但我找不到解决方案。

我有一个静态 CMS 页面,它显示了我的 Magento 商店中某个类别的产品列表。

我想包括一个添加到购物车按钮和一个数量框,以便用户可以将每个产品添加到他们的购物车中。

问题是我已经包含了“添加到购物车”按钮,但是单击它时不会将产品添加到购物车中。我已经读到我需要将产品 ID 包含到字符串中,但我使用 foreach 循环动态显示一个类别中的多个产品......

我的问题有解决方法或解决方案吗?任何想法都会受到欢迎。

这是代码:

              <?php 
$category_id = "49"; // category_id for "Accessories"
$_productCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect(array('name', 'price', 'small_image', 'short_description'), 'inner')
->addCategoryFilter(Mage::getModel('catalog/category')->load($category_id));
?>
<?php if($_productCollection->count()): ?>
  <ul class="media-list pull-left">
   <?php 
   $products = array();
   foreach ($_productCollection as $_product) { 
    ?>
    <li class="media">
      <a class="fancybox pull-left" href="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(220, 200); ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>">
        <img class="media-object"  src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(100, 100); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />
      </a>
    </li>
    <div class="media-body">
      <h4 class="media-heading"><a class="view-item-button" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->__('Order'); ?> <?php echo $this->htmlEscape($_product->getName())?><?php echo $this->__('&#8482;'); ?></a></h4>
      <div class="media">
       <?php echo $_product->_data['short_description']; ?>
       <div class="clearfix"></div>
       <h6>Price <?php echo Mage::helper('core')->currency($_product->getPrice());; ?></h6>
       <?php if($_product->isSaleable()): ?>
       <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="btn btn-danger btn-mini" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
     <?php else: ?>
     <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
   <?php endif; ?>

 </div>
</div> 
<?php } ?>
</ul> 
<script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])</script><?php endif; ?>
<script type="text/javascript">
$j(document).ready(function() {
  $j(".fancybox").fancybox();
});
</script>
4

3 回答 3

0

我可以使用以下块代码将产品添加到购物车:

 

 {{block type="catalog/product_list_related" category_id="49" template="PATH_TO_TEMPLATE_FILE"}}

然后在文件中添加一个结帐按钮,它就可以工作了!

`<?php if($_product->isSaleable()): ?>
            <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="btn btn-danger" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
                <?php else: ?>
            <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
        <?php endif; ?> `
于 2012-12-04T09:59:27.393 回答
0

您可以使用查询字符串来实现这一点,如此处所述

于 2012-12-03T14:40:34.137 回答
0
                 <?php 
$category_id = 49; // category_id for "Accessories"
$category = new Mage_Catalog_Model_Category();
$category->load($categoryid);
$_productCollection = $category->getProductCollection();
$_productCollection->addAttributeToSelect('*');
?>
<?php if($_productCollection->count()): ?>
  <ul class="media-list pull-left">
   <?php 
   $products = array();
   foreach ($_productCollection as $_product) { 
    ?>
    <li class="media">
      <a class="fancybox pull-left" href="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(220, 200); ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>">
        <img class="media-object"  src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(100, 100); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />
      </a>
    </li>
    <div class="media-body">
      <h4 class="media-heading"><a class="view-item-button" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->__('Order'); ?> <?php echo $this->htmlEscape($_product->getName())?><?php echo $this->__('&#8482;'); ?></a></h4>
      <div class="media">
       <?php echo $_product->_data['short_description']; ?>
       <div class="clearfix"></div>
       <h6>Price <?php echo Mage::helper('core')->currency($_product->getPrice());; ?></h6>
       <?php if($_product->isSaleable()): ?>
       <form id="addtocartform_<?=$_product->getId()?>" action="<?=Mage::helper('checkout/cart')->getAddUrl($_product)?>">
        <input type="hidden" value="<?=$_product->getId()?>" name="product" id="product"/>
        <?php if(!$_product->isGrouped()): ?>
        <input type="text" name="qty" id="qty" maxlength="12" value="1" />
        <?php endif; ?>
        <button type="button" class="button btn-cart" onclick="this.form.submit()"><span><span>Add To Cart</span></span></button>
       </form>
     <?php else: ?>
     <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
   <?php endif; ?>

 </div>
</div> 
<?php } ?>
</ul>  
<?php endif; ?>
于 2014-01-20T16:34:52.653 回答