Pregunta

I'm new for Magento. I need to display latest added product in home page. I'm using following code in 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>

It's working fine. But I can't correct code for add-to-cart option.

I found fllowing code from addtocart.phtml with script,

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

But, if I click add-to-cart, going to home page.

Help me.

¿Fue útil?

Solución

Call a block on home page through static block with content value as

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

or you can directly call this block on cms home , paste this

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

in your cms home page content.

create a file in folder frontend/theme_folder/default/template/catalog/product as latest.phtml.

Paste the below code in it as :

Latest Products

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

Thats it you will be able to see the latest 4 product on home page. you can increase the number of product by increasing the ->setPage(1, 4); to your required number of product on home page.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top