Tuesday, March 26, 2013

Special character of Regular Expression

Regular Expression :

>> Meaning of Special character [](){}|?+-*^$\

    []    => Square brackets can be used to define a character group. [abc] The example finds one of the characters a, b or c.   

    -    => The hyphen can be used to define a range of characters. [a-f] The example finds one of the characters a, b, c, d, e or f. Also in this example, the regular expression only matches one character.

    ^    => With the meta character ^ at the beginning of a character group, the character group is negated. That means, the example will match any character but not an a. [^a]

           ^a If the meta character ^ is not included into a character group, it stands for the beginning of a string or a line. The example would match all lines or strings beginning with a.

    $    => a$ Like the meta character ^ stands for the beginning of a string or a line, the character $ stands for its end. The example would match all strings or lines ending with an a.
   
            ^abc$ Here the meta characters ^ and $ are used together. This example would match all strings or lines which are equal to "abc".
            Example 1: abc
            Example 2: abc abc

Friday, March 22, 2013

Prevent visitors to view the store without login in Magento


>> Magento use methods :  _redirect() AND _redirectUrl()
for redirect in Mage_Core_Controller_Varien_Action controller. If you see these methods, they are using this code to redirect $this->getResponse()->setRedirect('your_url');

Now you just need to call like :
                        Mage::app()->getFrontController()
                               ->getResponse()
                               ->setRedirect(Mage::getBaseUrl('your_url');


I used this code to prevent visitors to view my store without login.
With the following code in head.phtml :

<?php
    $CusSession=mage::getSingleton('customer/session');
    $frontController = Mage::app()->getFrontController();
  
    if(!$CusSession->isLoggedIn() && $frontController->getRequest()->getActionName() != 'login')
    {
       $frontController->getResponse()->setRedirect(Mage::getUrl("customer/account/login"));
    }
?>




Wednesday, February 20, 2013

How to add image in magento admin product Grid


>> With the help of this code you can able to add product image in admin Grid View :

    Step 1:
    First you need to copy the core block to local. Copy Grid.php from app/code/core/Mage/Adminhtml/Block/Catalog/Product/Grid.php to
    app/code/local/Mage/Adminhtml/Block/Catalog/Product/Grid.php
    Here you'll find a method named _prepareColumns(), add the below code within this method
  
        $this->addColumn('product_image', array(
              'header'    => Mage::helper('catalog')->__('Image'),
              'align'     =>'left',
              'index'     => 'entity_id',
              'width'     => '100px',
              'renderer'  => 'Mage_Adminhtml_Block_Catalog_Product_Renderer_Image'
        ));
  

    Step 2:
    Now create the renderer file named Image.php in the following path
    app/code/local/Mage/Adminhtml/Block/Catalog/Product/Renderer/Image.php
    Here add the below code..
  
    class Mage_Adminhtml_Block_Catalog_Product_Renderer_Image extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
    {
        public function render(Varien_Object $row)
        {
            $_product = Mage::getModel('catalog/product')->load($row->getEntityId());
            if($_product->getImage() != 'no_selection'){
                  $image = "<img src='".Mage::helper('catalog/image')->init($_product, 'image')->resize(100)."' title='".$_product->getName()."' />";
            }
            return $image;
        }
    }
  
  
  
   

    

Tuesday, February 19, 2013

Use INR symbol in magento


 >> By default magento shows indian rupee as "Rs". If you want to show the symbol.


        &#8377; = ₹
        &#8360; = ₨

      i.e.  &#8377; 1,200 would show ₹ 1,200

     Note* : Use the font DejaVu Sans <span style='font-family: DejaVu Sans;'>&#8377;</span> for Internet Explorer and Safari.

>>  Go to Admin=>System=>Configuration=>Manage Currency=>Symbols. Replace the symbol.


>>  Here is a useful extension that will help you to replace rupee to new symbol. You can configure this one.

       http://www.magentocommerce.com/magento-connect/et-currency-manager.html


Tuesday, January 22, 2013

How to resize product/custom image in magento


>> Resize product image with product object.

          <img src="<?php echo Mage::helper('catalog/image')->init($_product, 'image')->resize(70,155);?>"/>


>> Resize a custom Image.

        // Image name
        $image = "";

        // actual image path
        $imageUrl = Mage::getBaseDir('media'). DS .'justonestepsolution'. DS .$image;
    
        // Give resized image path to be saved
        // here, the resized image will save in media/justonestepsolution/resized folder
        $imageResized = Mage::getBaseDir('media'). DS .'justonestepsolution'. DS .'resized'. DS .$image;
    
        // image will resize only if the image file exists and the resized image file doesn't exist

        if (!file_exists($imageResized) && file_exists($imageUrl))
        {
            $imageObj = new Varien_Image($imageUrl);
            $imageObj->constrainOnly(TRUE);
            $imageObj->keepAspectRatio(TRUE);
            $imageObj->keepFrame(FALSE);
            $imageObj->resize(300, null);
            $imageObj->save($imageResized);
        }


enjoy :-)



Tuesday, January 15, 2013

Get current logged customer details in magento


>>  $CusSession=mage::getSingleton('customer/session');
    //Checking is logged in customer
    if($CusSession->isLoggedIn())
    {
        $customer    = $CusSession->getCustomer();     // Customer object
        $name        = $customer->getName();
    }

Wednesday, January 9, 2013

Simple jQuery/html tab

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery Simple Tab</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function() {
     jQuery(".tab_content").hide();
     jQuery("ul.tabs li:first").addClass("active").show();
     jQuery(".tab_content:first").show();
  
     //On Click Event
     jQuery("ul.tabs li").click(function() {
         if(!jQuery(this).hasClass('active')){
            jQuery("ul.tabs li").removeClass("active");
            jQuery(this).addClass("active");
            jQuery(".tab_content").hide();
          
            var activeTab = jQuery(this).find("a").attr("href");
            jQuery(activeTab).fadeIn();
            return false;
         }else{
            return false;
         }
     });

});
</script>
<style type="text/css">
.details-tab-container{width: 800px; height:auto; position:absolute;}
.details-tab{ width:800px; float:left;}
.details-tab ul.tabs{ list-style-type:none;}
.details-tab li{background:#999; border:1px solid; float:left; padding:3px; margin:1px; border-radius: 6px;}
.details-tab li.active{background:#000;}
.details-tab li.active a{color:#FFF; font-size:14px; }
.details-tab li a{ text-decoration:none; font-family:Verdana, Geneva, sans-serif; font-weight:bold; font-size:11px; color:#000;}
.tab-container{ width:800px; float:left; border:2px solid;}
</style>
</head>

<body>
<div class="details-tab-container">
  <div class="details-tab">
    <ul class="tabs">
      <li><a href="#description"><span>Product Description</span></a></li>
      <li><a href="#attribute"><span>Product Attributes</span></a></li>
      <li><a href="#review"><span>Product Reviews</span></a></li>
    </ul>
  </div>
  <div class="tab-container">
    <div class="tab_content" id="description">Description Lorem ipsum dolor sit amet, tation intellegebat cum ea, at vidit splendide has, te sit ridens salutatus abhorreant. Eum te augue cetero. Clita interpretaris eos cu. Per no senserit suscipiantur. Altera molestie eos ad, pro no fierent periculis. Ponderum convenire sit no.</div>
    <div class="tab_content" id="attribute">Attributes Lorem ipsum dolor sit amet, tation intellegebat cum ea, at vidit splendide has, te sit ridens salutatus abhorreant. Eum te augue cetero. Clita interpretaris eos cu. Per no senserit suscipiantur. Altera molestie eos ad, pro no fierent periculis. Ponderum convenire sit no.</div>
    <div class="tab_content" id="review">Review Lorem ipsum dolor sit amet, tation intellegebat cum ea, at vidit splendide has, te sit ridens salutatus abhorreant. Eum te augue cetero. Clita interpretaris eos cu. Per no senserit suscipiantur. Altera molestie eos ad, pro no fierent periculis. Ponderum convenire sit no.</div>
  </div>
</div>
</body>
</html>

see the demo here..  :-)