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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 428
    Comment on it

    In magento if we are required to create a simple slider of products which are recently edited 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 Editedslider.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"?>
    <config>
      <modules>
        <Namespace_Module>
          <version>0.1.0</version>
        </Namespace_Module>
      </modules>
      <frontend>
        <layout>
          <updates>
            <module>
              <file>editedslider.xml</file>
            </module>
          </updates>
        </layout>
      </frontend>
      <global>
        <helpers>
          <module>
            <class>Namespace_Module_Helper</class>
          </module>
        </helpers>
        <blocks>
          <module>
            <class>Namespace_Module_Block</class>
          </module>
        </blocks>
      </global>
    </config>


    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 editedslider.xml which is in the layout folder of our theme.

     

    Now, Block is created to load the products which are recently edited for say between from last month to the current month . Create a Block with the name Editedslider.php and add the below code in it.

    <?php
    
    class Namespace_Module_Block_Editedslider 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()
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('updated_at', array('gteq' =>$first))
            ->addAttributeToFilter('updated_at', array('lteq' => $last))
            ->load();
    
        return $products;
      }
    
    }
    
    ?>

    In the above code we are loading the products which are recently edited 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 'updated_at' between these dates and returned the products array.

     

    Now at the path design/frontend/yourtheme/layout create your layout file editedslider.xml with the below code.

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

    in the above we are calling our own block in the home page for that we have defined the clock between <cms_index_index> tag.

    Now the thing is we have to design the slider in our block at /design/frontend/yourtheme/template/editedslider/sliderblock.phtml
    for the slider write the below code :

    <?php
     $products = $this->ftotalProduct();
     $prodIds = $products->getAllIds();
     $ftotalproduct = count($prodIds);
    ?>
      <div class="featured">
        <div class="fhead">
          <strong>
            <span>
              <?php echo $this->__('Recently Edited Products'); ?>
            </span>
          </strong>
        </div>
        <?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()) ?>" >
                      <span>
                        <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(170) ?>" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" />
                      </span>
                    </a>
                    <span class="productname">
                      <a href="<?php echo $this->getUrl($_product->getUrlPath()) ?>" ><?php echo $_product->getName(); ?>
                      </a>         
                    </span>
                    <span class="price-con"><?php echo Mage_Catalog_Block_Product::getPriceHtml($_product, true) ?></span>
                    <span class="fadd">
                      <p>
                        <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>
                      </p>
                    </span>
                  </li>
            <?php endforeach; ?>
            </ul>
          </div>
        </div>
      <?php else: ?>
        <div class="show-msg">
          <?php echo $this->__("There are no feature products."); ?>
        </div>
      <?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 is to use magento default slider.
    in that way we can define our own slider by changing the block file query to fetch the product according to our requirement.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
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: