Latest Product with Add-to-Cart on Home Page – Magento Guide

cartproduct-list

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.

Please, help me.

Best Answer

Welcome to magento.

Unfortunately your implementation of the functionality is very wrong :( You should not be placing logic as this in the actual template(s), especially not the base root template 2columns-left.phtml (eeeek) If any other page uses this root template, your code will also run there, thus not just the homepage.

You should create a module, with blocks, and insert the blocks via magento's layout system into the pages you require as child html block.

Regarding your issue, (I have not checked this, but I am sure of it) - your action for the post is calling $this->getSubmitUrl(), which is not a valid method for that block. In fact, I don't think the line above that $_product = $this->getProduct(); would yield any result as well.

Your collection looks a bit odd as well. (or rather overcomplicated) If you just want to display one (latest added) product, all you need to do is order a collection of products descending, limit to 1, and then you got the latest added. (product ids are sequential)

Thus you could do:

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
            ->addFieldToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
$collection->getSelect()->order('entity_id desc')->limit(1);

I highly suggest you read these tutorials:

http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-4-magento-layouts-blocks-and-templates

to understand how layouts, blocks, and templates work.

I also highly suggest you google for some tutorials on how to create magento modules.

Ok, so now, lets see how to create what you need.

Magento has core functionality that displays NEW products. See the Core block Catalog/product/new.php

This block has the method 'protected function _getProductCollection()' which builds the product collection for NEW products.

So, al you need to do is create your own module (lots of tutorials via google how to create a module) and in that module have a block based off this core block (simply copy that block new.php to your module and call it latest.php, and update the class name)

Then change the method that builds the collection to a collection similar to what I had noted above, which will give you the latest product. (or products(s) if you increase the limit in the collection)

Now, the last thing to do is to copy the template file that goes with the new.php class (design/frontend/base/default/template/catalog/product/new.phtml) to your own theme, named to latest.phtml (seems like a cool name ;) ) Adjust the template to your style/theme.

And in the homepage cms block you can do this to display the block/template

{{block type=”YOUR_MODULE/product_latest” name=”home.catalog.product.latest” alias=”product_homepage_latest” template=”catalog/product/latest.phtml”}}

Bang - done, with limited of custom code :)

Good luck, and please ask away as you continue your exploration of magento.