Thursday 28 November 2013

Toggle Visibility - Show/Hide use in javascript


<script type="text/javascript">
<!--
    function toggle_visibility(id) {
       var e = document.getElementById(id);
       if(e.style.display == 'block')
          e.style.display = 'none';
       else
          e.style.display = 'block';
    }
//-->
</script>

==============================HTML============================
 <div class="test" onclick="toggle_visibility('testId');" >Click Me</div>
<div id="testId">
Demo text
</div>

Tuesday 26 November 2013

Product collection filter by attribute value

$productCollection = Mage::getModel('catalog/product')->getCollection();

 $productCollection->addAttributeToFilter('color', 4);

For example   attribute code  ="color"
                           Red (value id) ="4"

Thursday 10 October 2013

Passing array of checkbox values to php through jQuery (example)


<!DOCTYPE html>
<html>
<head>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
        <script type="text/javascript">
                function doit() {
                        var p=[];
                        $('input.cb').each( function() {
                                if($(this).attr('checked')) {
                                        p.push($(this).attr('rel'));
                                }
                        } );
                        $.ajax( {
                                url:'/process.php',
                                type:'POST',
                                data: {list:p},
                                success: function(res) {
                                        alert(res);
                                }
                        });
                }
        </script>
</head>
<body>
        <input type="checkbox" class="cb" rel="1"></input>This is 1<br />
        <input type="checkbox" class="cb" rel="abc"></input>This is abc<br />
        <input type="checkbox" class="cb" rel="999"></input>This is 999<br />
        <a href="javascript:void(0)" onclick="doit()">Click</a>
</body>
</html>

print_r(@$_POST['list']);

Friday 20 September 2013

Magento Add ‘Customer Group’ to Registration Form

Step -1
customer/form/register.phtml Add


<div>
<label for=”group_id”><?php echo $this->__(‘Group’) ?><span class=”required”>*</span></label><br/>
<select name=”group_id” id=”group_id” title=”<?php echo $this->__(‘Group’) ?>” class=”validate-group required-entry input-text” />
<?php $groups = Mage::helper(‘customer’)->getGroups()->toOptionArray(); ?>
<?php foreach($groups as $group){ ?>
<option value=”<?php print $group['value'] ?>”><?php print $group['label'] ?></option>
<?php } ?>
</select>
</div>

Step-2

Mage\Customer\controllers\AccountController.php
line no 287 add
$customer->setGroupId($this->getRequest()->getPost(‘group_id’));

Step-3

Mage\Customer\etc\config.xml
Add customer_account node
<group_id><create>1</create><update>1</update></group_id>


Thursday 19 September 2013

Creating extra fields in registration form in magento

Link:- http://www.webtechnologycodes.com/creating-extra-fields-in-registration-form-in-magento/


Code:- Step1.
Open your register.phtml file in magento.
if you want to add such as office address fields in your form
then insert this line


<div class="field">
                    <label for="offaddress" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Office Address') ?></label>
                    <div class="input-box">
                        <input name="offaddress" id="offaddress" title="<?php echo Mage::helper('contacts')->__('Office Address') ?>" value="<?php echo $this->escapeHtml($this->getFormData()->getOffaddress()) ?>" class="input-text required-entry" type="text" />
     </div>
            </div>



Step2.

Now to retrieve this field in my account page 

<li>
            <label for="offaddress" class="required"><em>*</em><?php echo $this->__('Office Address') ?></label>
            <div class="input-box">
               <input type="text" name="offaddress" id="offaddress" value="<?php echo $this->htmlEscape($this->getCustomer()->getOffaddress()) ?>" title="<?php echo $this->__('Office Address') ?>" class="input-text required-entry validate-email" />
            </div>
        </li>


Step 3. Now the final step is to create that type of attribute in database

for this run this script once in your register.phtml file and you are all done.


<?php
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttribute('customer', 'offaddress', array(
 'label'  => 'Office Address',
 'type'  => 'varchar',
 'input'  => 'text',
 'visible' => true,
 'required' => false,
 'position' => 1,
 ));
$eavConfig = Mage::getSingleton('eav/config');
$attribute = $eavConfig->getAttribute('customer', 'offaddress');
$attribute->setData('used_in_forms', array('adminhtml_customer','customer_account_create','customer_account_edit'));  //enable all action
//$attribute->setData('used_in_forms', array('adminhtml_customer'));  //to make the attribute can be created in backend only
//$attribute->setData('used_in_forms', array('customer_account_create')); //to make the attribute can be created in registration only
//$attribute->setData('used_in_forms', array('customer_account_edit')); // to make the attribute can be edited in the frontend only

$attribute->save();
?>

Wednesday 18 September 2013

How to cuntry code respect to set currency

LINK:- http://rosskendall.com/blog/web/using-geoip-with-magento-and-multiple-sites


code:- //GeoIP Track
define("GEOIP_DAT_FILE", "geoip/GeoIP.dat");
define("GEOIP_INC_FILE", "geoip/geoip.inc");

include(GEOIP_INC_FILE);

$_geoip = geoip_open(GEOIP_DAT_FILE ,GEOIP_STANDARD);
$_country_code = geoip_country_code_by_addr($_geoip, $_SERVER['REMOTE_ADDR']);

geoip_close($_geoip);

switch ($_country_code) {
  case "GB": // United Kingdom
    $_SERVER["MAGE_RUN_CODE"] = "uk";
    break;
  case "AU": // Australia
    $_SERVER["MAGE_RUN_CODE"] = "au";
    break;
  case "NZ": // New Zealand
    $_SERVER["MAGE_RUN_CODE"] = "nz";
    break;
  default:
    $_SERVER["MAGE_RUN_CODE"] = "";
}

Wednesday 1 May 2013

Custom pagination


<?php
/**
* Array Pagination Function.
* By Sergey Gurevich.
*
* Input:
* 1 - Target Array.
* 2 - Page Number.
* 3 - Link prefix (example: "?page=").
* 4 - Link suffix.
* 5 - Results per page.
* 6 - Number of pages displayed in the page link panel.
*
* Output:
* - $output['panel'] - Link Panel HTML source.
* - $output['offset'] - Current page number.
* - $output['limit'] - Number of resuts per page.
* - $output['array'] = - Array of current page results.
*
* Will return FALSE if no pagination was done.
*/
function pagination_array($array, $page = 1, $link_prefix = false, $link_suffix = false, $limit_page =5, $limit_number = 5)
{
if (empty($page) or !$limit_page) $page = 1;

$num_rows = count($array);
if (!$num_rows or $limit_page >= $num_rows) return false;
$num_pages = ceil($num_rows / $limit_page);
$page_offset = ($page - 1) * $limit_page;

//Calculating the first number to show.
if ($limit_number)
{
$limit_number_start = $page - ceil($limit_number / 2);
$limit_number_end = ceil($page + $limit_number / 2) - 1;
if ($limit_number_start < 1) $limit_number_start = 1;
//In case if the current page is at the beginning.
$dif = ($limit_number_end - $limit_number_start);
if ($dif < $limit_number) $limit_number_end = $limit_number_end + ($limit_number - ($dif + 1));
if ($limit_number_end > $num_pages) $limit_number_end = $num_pages;
//In case if the current page is at the ending.
$dif = ($limit_number_end - $limit_number_start);
if ($limit_number_start < 1) $limit_number_start = 1;
}
else
{
$limit_number_start = 1;
$limit_number_end = $num_pages;
}
//Generating page links.
for ($i = $limit_number_start; $i <= $limit_number_end; $i++)
{
$page_cur = "<a href='$link_prefix$i$link_suffix'>$i</a>";
if ($page == $i) $page_cur = "<b>$i</b>";
else $page_cur = "<a href='$link_prefix$i$link_suffix'>$i</a>";

$panel .= " <span>$page_cur</span>";
}



$panel = trim($panel);
//Navigation arrows.
if ($limit_number_start > 1) $panel = "<b><a href='$link_prefix".(1)."$link_suffix'>&lt;&lt;</a>  <a href='$link_prefix".($page - 1)."$link_suffix'>&lt;</a></b> $panel";
if ($limit_number_end < $num_pages) $panel = "$panel <b><a href='$link_prefix".($page + 1)."$link_suffix'>&gt;</a> <a href='$link_prefix$num_pages$link_suffix'>&gt;&gt;</a></b>";

$output['panel'] = $panel; //Panel HTML source.
$output['offset'] = $page_offset; //Current page number.
$output['limit'] = $limit_page; //Number of resuts per page.
$output['array'] = array_slice($array, $page_offset, $limit_page, true); //Array of current page results.

return $output;
}
?>

<?php

for ($i = 1; $i <= 100; $i++)
{
$array[] = "Result #$i";
}

//Getting currect page.
$page = $_GET['page'];

//Applying pagination.
$pagination = pagination_array($array, $page, "?page=");

//Page link panel.
echo $pagination['panel'];

//Displayed page results.
echo '<pre>';
print_r($pagination['array']);

//Page link panel.
echo $pagination['panel'];
echo "<br/>";
 echo $pagination['offset'];
 foreach($pagination['array'] as $val) {
    echo "<br>V : ".$val;
}

?>

Follow the link :-
http://www.codehive.net/PHP-Array-Pagination-10.html#comments_table

PHP Array Pagination link

http://www.codehive.net/PHP-Array-Pagination-10.html#comments_table

Tuesday 23 April 2013

Add first/last name in newsletter module Magento

 >> First add two fields ( subscriber_firstname, subscriber_lastname) in newsletter_subscriber table and app\design\frontend\THEME\default\template\newsletter\subscribe.phtml.

    >> Now edit this page : app\code\core\Mage\Newsletter\controllers\SubscriberController.php

        Here you'll find "public function newAction()"
            Paste the below code after this following code:
            $status = Mage::getModel('newsletter/subscriber')->subscribe($email);    //line (63)

     ///////////////////////// First Name //////////////////////////////

            if ($this->getRequest()->getPost('subscriber_firstname'))
                {
                     $subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($email);
                     $name     = (string) $this->getRequest()->getPost('subscriber_firstname');
                     $subscriber->setSubscriberFirstname($name);
                     $subscriber->save();
                }

     //////////////////////  END  ///////////////////////////////////////

            Do same for field Last Name....

    >> The above code will effect your frontend. If you want to see the information from Admin too, you have to implement a little bit.

    Go to this link : app\code\core\Mage\Adminhtml\Block\Newsletter\Subscriber\grid.php
    Add this code :

    //It will show the information to the admin on News Letter Subscriber Section.
    $this->addColumn('subscriber_firstname', array(
            'header'    => Mage::helper('newsletter')->__('Subscriber First Name'),
            'index'     => 'subscriber_firstname',
            'default'   =>    '----'
        )); 

   

                                        ***********         Good Luck        **********

On click text remove

onclick="if(value=='Last name') value = ''" onblur="if(value=='') value = 'Last name'"

Generating a random password in php


   function rand_string( $length ) {
                   
                        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
                        return substr(str_shuffle($chars),0,$length);
                   
                    }
echo rand_string(7);

Thursday 4 April 2013

Magento get category level


<?php
// get category level, assuming that you have known category object, $_category
echo $categoryLevel = $_category->getLevel();

?>

Wednesday 3 April 2013

How to Change the Order Increment ID and Prefix in Magento

http://www.warpconduit.net/2012/04/18/how-to-change-the-order-increment-id-and-prefix-in-magento/



UPDATE eav_entity_store
INNER JOIN eav_entity_type ON eav_entity_type.entity_type_id = eav_entity_store.entity_type_id
SET eav_entity_store.increment_last_id='XXXXXXXXXX'
WHERE eav_entity_type.entity_type_code='order' AND eav_entity_store.store_id = 'Y';

Monday 1 April 2013

category thumbnail images


<?php  $imageUrl = Mage::getBaseUrl('media')
                . 'catalog/category/'
                . $_subcat->getData('thumbnail');?>

Thursday 14 March 2013

Attribute search


   <?php 
                           $attribute = Mage::getModel('eav/entity_attribute')
                           ->loadByCode('catalog_product', 'manufacturer');
                           $valuesCollection = Mage::getResourceModel('eav/entity_attribute_option_collection')
                           ->setAttributeFilter($attribute->getData('attribute_id'))
                           ->setPageSize(100)
                           ->setPositionOrder('asc')
                           ->setStoreFilter(0, false);

                           $preparedManufacturers = array();            
                           foreach($valuesCollection as $value) {
                                    $preparedManufacturers[$value->getOptionId()] = $value->getValue();
                           }   
                                    if (count($preparedManufacturers)) {
                                             foreach($preparedManufacturers as $optionId => $value) {
                                             ?>
                                             <li><a href="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB); ?>catalogsearch/advanced/result/?manufacturer[]=<?php echo $optionId; ?>"><?php echo $value; ?></a></li>
                                             <?php
                                             }
                                    }
                           ?>

Wednesday 20 February 2013

Magento Product Collection By category id

<?php
$categoryid=2;
                               
                                $collection = Mage::getModel('catalog/category')->load($categoryid)
                                ->getProductCollection()
                                ->addAttributeToSelect('*') // add all attributes - optional
                                ->addAttributeToFilter('status', 1) // enabled
                                ->addAttributeToFilter('visibility', 4) //visibility in catalog,search
                                ->addAttributeToFilter('featured', 1)
                                ->setOrder('position', 'ASC')
                                ->setPageSize(6)
                                ->setCurPage(1);
                         foreach($collection as $_product){
   $productID=$_product->getId();
   $_product = Mage::getModel('catalog/product')->load($productID);
                                    echo $_product->getName();
}
?>

Thursday 7 February 2013

text wordwrap demo


<?php
    $val="rem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
    $str=wordwrap($val,30,"<br>");
                                                
    echo substr($str,0,strpos($str,"<br>"))."..." ; 
?>

Magento database feache code


<?php
     $write = Mage::getSingleton('core/resource')->getConnection('core_write');

         $row = $write->fetchRow('SELECT * FROM colings_eav_attribute_option_value WHERE value="'.$lens_col.'"'); ?>

Attribute option value table name

eav_attribute_option_value

Thursday 31 January 2013

Friday 18 January 2013

Feature Product check yes/on

<?php
 $featured_products=Mage::getModel('catalog/product')
                                         ->getCollection()
                                         ->addAttributeToFilter('status', 1)
                                         ->addAttributeToFilter('visibility', 4)
                                         ->addAttributeToFilter('featured', 1);

?>

Thursday 17 January 2013

Friday 11 January 2013

How to call staic block any XML file


<reference name="left">
    <block type="cms/block" name="sample_block" before="-">
        <action method="setBlockId"><block_id>sample_block</block_id></action>
    </block>   
</reference>

How to call image in static block

{{skin url='images/media/about_us_img.jpg'}}

How to call staick block in your template or .phtml files

<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('my_static_block_name')->toHtml() ?>

Wednesday 9 January 2013

Phtml page call in cms page in magento

{{block type="core/template"  template="custompage/homecontent.phtml"}}

Product Type in magento


<?php  $productype=$_product->getTypeID(); ?>

Product Available (In stock/Out of stock)


<?php
         $thisProduct = Mage::getModel('catalog/product')->load($_product->getId());
         if($thisProduct->isAvailable()):
            echo $this->__('In stock');
         else:
            echo $this->__('Out of stock');
         endif;
    ?>