简体   繁体   中英

add products to cart from cms page magento

I have a relatively small problem but I cannot find the solution.

I have a static CMS page which shows a list of products from a category in my Magento store.

I would like to include an add to cart button and a quantity box as well so users can add each product to their shopping cart.

The problem is I have included the add to cart button but when clicked it doesn't add a product to cart. I've read that I need to include the product id into the string but I am displaying more than one product from a category dynamically using a foreach loop....

Is there a workaround or solution to my problem? Any ideas will be welcomed.

Here's the code:

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

你可以使用查询字符串来实现这一点,因为解释这里

I am able to add a product to cart by using the following block code:

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

Then add aa checkout button to the file and it works!

`<?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; ?> `
                 <?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; ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM