Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • How to create slider of products which are recently added ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 385
    Comment on it

    In magento if we are required to create a simple slider of products which are recently Added for say between from previous month to the current month.
    Lets see how we can create it :

    1. Create a module with namespace and module.
    2. Then Create a file config.xml in etc folder in our module.
    3. After that create a block file Addedslider.php in Block folder in our module.
    3. finally create a Helper with the file Data.php.
    Now, in our config.xml file at the path Namespace/Module/etc/config.xml write the below code in it :

    <?xml version="1.0"?>

    In the above code we have defined our module with the version 0.1.0 and 
    then in frontend we are calling our new layout file addedslider.xml which is in the layout folder of our theme.
    Now, Block is created to load the products which are recently added for say between from last month to the current month . Create a Block with the name Addedslider.php and add the below code in it.

    class Namespace_Module_Block_Addedslider extends Mage_Core_Block_Template {
      public function ftotalProduct() {
        $first = date('Y-m-01', strtotime('-1 months'));
        $first = $first.''.'00:00:00';
        $last = date('Y-m-d');
        $last = $last.''.'00:00:00';
        $collection = Mage::getModel('catalog/product');
        $products = $collection->getCollection()
            ->addAttributeToFilter('created_at', array('gteq' =>$first))
            ->addAttributeToFilter('created_at', array('lteq' => $last))
        return $products;

    In the above code we are loading the products which are recently added for this we first took the dates between previous month to the current month date.
    then we wrote the query to fetch products which have field values 'created_at' between these two dates and returned the products array.
    Now at the path design/frontend/yourtheme/layout create your layout file addedslider.xml with the below code.

    <?xml version="1.0"?>
    <layout version="0.1.0">
        <reference name="content">
          <block type="addedslider/addedslider" name="addedslider" as="addedslider" template="addedslider/addedslider.phtml" />

    in the above we are calling our own block in the home page for that we have defined the block between <cms_index_index> tag.
    Now the thing is we have to design the slider in our block at /design/frontend/yourtheme/template/addedslider/addedslider.phtml

    for the slider write the below code :

      $products = $this->ftotalProduct();
      $prodIds = $products->getAllIds();
      $ftotalproduct = count($prodIds);
      <div class="featured">
        <div class="fhead">
              <?php echo $this->__('Recently Edited Products'); ?>
        <?php if ($ftotalproduct): ?>
          <div class="slideshow-container" style="width:30%;">
            <ul class="slideshow">
              <?php foreach ($products as $_product) : ?>
                  <li class="productdiv">
                    <a href="<?php echo $this->getUrl($_product->getUrlPath()) ?>" >
                        <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(170) ?>" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" />
                    <span class="productname">
                      <a href="<?php echo $this->getUrl($_product->getUrlPath()) ?>" ><?php echo $_product->getName(); ?>
                    <span class="price-con"><?php echo Mage_Catalog_Block_Product::getPriceHtml($_product, true) ?></span>
                    <span class="fadd">
                        <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation(' <?php echo $this->helper('checkout/cart')->getAddUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
            <?php endforeach; ?>
      <?php else: ?>
        <div class="show-msg">
          <?php echo $this->__("There are no feature products."); ?>
      <?php endif; ?>

    In the above code we are using class="slideshow-container" in the div before ul tag and class class="slideshow" in the ul tag, these two classes "slideshow-container" and  "slideshow"   are to use magento default slider.
    In that way we can define our own slider by changing the block file query to fetch the products according to our requirement.

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: