Monday, August 12, 2013

Magento add datepicker to frontend

Code referece:

http://stackoverflow.com/questions/8335456/magento-how-do-i-add-a-frontend-date-picker-that-returns-dd-mm-yy-instead-of-m


STEPS:
1. Add js and css necessary for the datepicker in frontend
<reference name="head">
        <action method="addItem"><type>js_css</type><name>calendar/calendar-win2k-1.css</name><params/><!--<if/><condition>can_load_calendar_js</condition>--></action>
        <action method="addItem"><type>js</type><name>calendar/calendar.js</name><!--<params/><if/><condition>can_load_calendar_js</condition>--></action>
        <action method="addItem"><type>js</type><name>calendar/calendar-setup.js</name><!--<params/><if/><condition>can_load_calendar_js</condition>--></action>
</reference>
 
2.
 <block type="core/html_calendar" name="html_calendar" as="html_calendar" template="page/js/calendar.phtml"/> 

3.call that block in the phtml file where u want to implement datepicker.
<?php echo $this->getChildHtml('html_calendar') ?>
 
4. Last use the below code:
 
<script type="text/javascript">
  Calendar.setup({
  inputField : 'deliverydate',
  ifFormat : '%m/%e/%y',
  button : 'date_from_trig',
  align : 'Bl',
  singleClick : true
  });
</script> 

Tuesday, June 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>

Friday, May 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.

Monday, April 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 Let me explain 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'.





Friday, April 26, 2013

Monday, April 8, 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;

Sunday, February 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();
                 }

}