6/18/2013

Magento signin signout links on any phtml file

 <ul class="links si_ac">
            <?php
            $signout =$baseurl.'customer/account/logout';
            if(!Mage::getSingleton('customer/session')->isLoggedIn()) {
            $signinUrl =$baseurl.'customer/account/login';
            $signup   =$baseurl.'customer/account/create';
             $linkks = "<li><a href='$signinUrl'>".$this->__("Sign In")."</a></li>
                <li class='last'><a  href='$signup'>".$this->__("Create an Account")."</a></li>";
            }else{
              $linkks = "<li class='last'><a  href='$signout'>".$this->__("Sign Out")."</a></li>";
            }
            echo $linkks;
            ?> 
         </ul>

5/17/2013

magento display multi select attribute in admin grid with filter



I have created a custom module by name supplier , and displayed several supplier on the product page in admin like in the image above, Now the multiselected values were not appearing properly in the grid page.

It was appearing empty for multiselected values. although i managed to display single selected values properly by the post:

http://blog.chapagain.com.np/magento-how-to-search-or-filter-by-multiselect-attribute-in-admin-grid/

But still there was problem with multi selected values, so i defined "renderer"
$this->addColumn('supplier', array(
            'header'    => Mage::helper('catalog')->__('supplier'),
            'width'     => '180px',
            'index'     => 'supplier',
            'type'  => 'options',
            'options' => $item_types,
             'filter_condition_callback' => array($this, '_filterSupplierCallback'),
            'renderer'=>new Mage_Adminhtml_Block_Catalog_Product_Columns_Supplier()        ));

IN THE RENDERER I USED BELOW CODE TO RETURN THE LIST OF MULTI SELECTED SUPPLIER:

public function render(Varien_Object $row)
    {
        $suppliers=explode(',',$row->getSupplier());
        $suppstr="";
        if(count($suppliers)>0)
        {
            $suppstr="<ul>";
            foreach($suppliers as $value)
            {
                $productModel = Mage::getModel('catalog/product');
                $attr = $productModel->getResource()->getAttribute("supplier");
                if ($attr->usesSource()) {
                $suppstr   .= "<li>".$attr->getSource()->getOptionText("$value")."</li>";
                }

            }
            $suppstr   .= "</ul>";
  
        }
          return $suppstr;   
       
    }



And successfully displayed multiselected options in the grid.

4/29/2013

magento add custom validation to admin section field

well... i had to create my own custom validation class for my magento extension because the client said that.
'For Video title- it should accept alphabets[A-Za-z], numerics and also special charater "Space" , and also it should not start with number'.
since changing core file is a bad habit , so i created  my own js file and add my custom validation class.But first let me tell u how u can do it fast by modifying core file.
//BAD BUT FAST WAY...
open  js/prototype/validation.js

some where at line 479-771, u will see lots of validation classess that magento uses. U can define your own validation class like i did.

I added below code after line 489.

  ['validate-alphanum-with-spaces-char-first', 'Please use only letters (a-z or A-Z), numbers (0-9) or spaces only in this field, first character should be a letter.', function(v) {
                return Validation.get('IsEmpty').test(v) ||  /^[a-z]+[a-z0-9 ]+$/.test(v)
            }],


 //NOW THE STANDARD WAY....
1. add your custom js file to the module.
     in my case it was video edit section in admin:

 app/design/adminhtml/default/default/layout/mymod_video.xml
<layout>
<adminhtml_video_video_edit>
     <reference name="head">
                    <action method="addJs"><script>alfasoft/videoext/myvalidation.js</script></action>
        </reference>

        <reference name="content">
          ....
        </reference>
        <reference name="left">
          ....
        </reference>
    </adminhtml_video_video_edit>

</layout>

2. upload your js i.e. myvalidation.js to the location js/alfasoft/videoext/

3. Add your code to myvalidation.js
   if(Validation)
    {
        Validation.addAllThese([
           
            ['validate-alphanum-with-spaces-char-first', 'Please use only letters (a-z or A-Z), numbers (0-9) or spaces only in this field, first character should be a letter.', function(v) {
                return Validation.get('IsEmpty').test(v) ||  /^[a-z]+[a-z0-9 ]+$/.test(v)
            }]     // this is my validation requirement, your could be anything else.


        ]);
    }

now look at the code. according to my requirement.

>the regular expression fit the best /^[a-z]+[a-z0-9 ]+$/.test(v) . your could be anything else.
>here my validation class name is  'validate-alphanum-with-spaces-char-first'. 
>And my error message is:
'Please use only letters (a-z or A-Z), numbers (0-9) or spaces only in this field, first character should be a letter.'

 Use your class in the form:
mine path was:
app/code/community/Alfasft/Video/Block/Adminhtml/Video/Edit/Tab/Form.php

$fieldset->addField('vtitle', 'text', array(
            'label' => Mage::helper('video')->__('Video Title'),
            'name'  => 'vtitle',
            'required'  => true,
            'class' => 'required-entry validate-alphanum-with-spaces-char-first',

        ));


 //I have assign this class to the field 'vtitle'.





4/26/2013

4/08/2013

magento admin redirect to same url after post

 Put the below code at the end of the action:

$this->_redirectReferer($this->getRequest()->getServer('HTTP_REFERER'));
        return;

2/10/2013

GET QUOTE ITEMS PRODUCT COUNT BY ATTRIBUTE


This code is for counting the total number to products in the cart having specific attribute code.

$items =Mage::getModel('checkout/cart')->getQuote()->getAllItems();
  $attriubtecnt=0;
 
   foreach($items as $item)
    {
$_product = Mage::getModel('catalog/product')->load($item->getProductId());
$att_value= $_product->getAttributeText('attributecode');

if(trim($att_value)=='lining')
{
                      $liningcnt +=$item->getTotalQty();
                 }

}

11/21/2012

MAGENTO CHANGE CAPTCHA BACKGROUND LINES AND DOTS DENSITY

I had to reduce the density of dots and lines in captcha because it was un readable sometimes.

so here what i did.

Go to:

lib/Zend/Captcha/Image.php

protected $_dotNoiseLevel = 10;
protected $_lineNoiseLevel = 0;