从cms page magento将产品添加到购物车

时间:2012-12-03 14:03:24

标签: php magento

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

我有一个静态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>

3 个答案:

答案 0 :(得分:0)

您可以使用查询字符串来实现此目标,如here

所述

答案 1 :(得分: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; ?> `

答案 2 :(得分: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; ?>