Apr 18, 2012
kalpesh

Magento: Get sentence case from camel case string

Magento has pre-defined methods for getters and setters which will convert camelcase string to underscore formatted string.
Example, Magento will convert thisIsDummy to this_is_dummy using it’s method _underscore which is available at Varien_Object class.

If you want to convert Camel case string to Sentence case (this may sound weird, but we got the requirement),
Example, from thisIsDummy to This Is Dummy, below method will help you to do so:

1
2
3
4
5
6
public function camelToSentence($word) {
            if(isset($word)) {
                $result = ucwords(preg_replace('/(.)([A-Z])/', "$1 $2", $word));
                return $result;
            }
        }
Apr 15, 2012
kalpesh

Magento: Difference between source_model, frontend_model, backend_model

When dealing with Magento Admin’s system configuration, you may have encountered with these models. If you don’t know, these models are used when you add custom fields through your system.xml file at Magento’s backend configuration (Admin -> System -> Configuration).

Okay, so the difference between these 3 models are:

source_model:
It specifies a Model class, where you will be returned with options that can populate in the current field.

Example,

1
<source_model>adminhtml/system_config_source_yesno</source_model>

this will populate select dropdown with values Yes/No

frontend_model:
It specifies a Block class to add custom render to use as a frontend field type instead of/along with default frontend_type field value (text, textarea, select, etc.).

Example, Continue reading »

Feb 29, 2012
kalpesh

PHP Convert SimpleXML object to Array

In PHP, if you want to convert your xml data to array for looping and displaying purpose, it sometimes becomes tedious. If you already have a SimpleXML class and want to convert XML to Array without any notices, warnings and recursively preparing the array, here is the method that will do so without any modifications needed. This will work by pasting this in your class file, if you are not using object oriented approach, you will have to remove “public” and “$this->” from this function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
public function simpleXMLToArray(SimpleXMLElement $xml,$attributesKey=null,$childrenKey=null,$valueKey=null){

    if($childrenKey && !is_string($childrenKey)){$childrenKey = '@children';}
    if($attributesKey && !is_string($attributesKey)){$attributesKey = '@attributes';}
    if($valueKey && !is_string($valueKey)){$valueKey = '@values';}

    $return = array();
    $name = $xml->getName();
    $_value = trim((string)$xml);
    if(!strlen($_value)){$_value = null;};

    if($_value!==null){
        if($valueKey){$return[$valueKey] = $_value;}
        else{$return = $_value;}
    }

    $children = array();
    $first = true;
    foreach($xml->children() as $elementName => $child){
        $value = $this->simpleXMLToArray($child,$attributesKey, $childrenKey,$valueKey);
        if(isset($children[$elementName])){
            if(is_array($children[$elementName])){
                if($first){
                    $temp = $children[$elementName];
                    unset($children[$elementName]);
                    $children[$elementName][] = $temp;
                    $first=false;
                }
                $children[$elementName][] = $value;
            }else{
                $children[$elementName] = array($children[$elementName],$value);
            }
        }
        else{
            $children[$elementName] = $value;
        }
    }
    if($children){
        if($childrenKey){$return[$childrenKey] = $children;}
        else{$return = array_merge($return,$children);}
    }

    $attributes = array();
    foreach($xml->attributes() as $name=>$value){
        $attributes[$name] = trim($value);
    }
    if($attributes){
        if($attributesKey){$return[$attributesKey] = $attributes;}
        else{$return = array_merge($return, $attributes);}
    }

    return $return;
}

Hope this helps.

Feb 12, 2012
kalpesh

Magento add admin user in MySQL

In Magento, if you want to create a new user directly in Mysql, it’s not that easy to insert one record in admin_user table.
You need to also update the privileges and inserting new admin’s roles.
So here is a Mysql script which will create a new admin user with all privileges.

Replace FIRSTNAME, LASTNAME, EMAIL, USERNAME, PASSWORD with your desired values.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
insert into admin_user
select
(select max(user_id) + 1 from admin_user) user_id,
'FIRSTNAME' first_name,
'LASTNAME' last_name,
'TEST@EMAIL.COM' email,
'USERNAME' username,
MD5('PASSWORD') password,
now() created,
NULL modified,
NULL logdate,
0 lognum,
0 reload_acl_flag,
1 is_active,
(select max(extra) from admin_user where extra is not null) extra,
NULL,
NULL;

insert into admin_role
select
(select max(role_id) + 1 from admin_role) role_id,
(select role_id from admin_role where role_name = 'Administrators') parent_id,
2 tree_level,
0 sort_order,
'U' role_type,
(select user_id from admin_user where username = 'USERNAME') user_id,
'USERNAME' role_name
Feb 12, 2012
kalpesh

Magento: Show “track your order” in frontend – My Orders

It is always required for the customer to track their order. The shipping carriers can be anything: Aramex, Bluedart, DHL, First Flight, Federal Express, etc.. Navigate to My Account and place a button in My Orders section there “Track Order”. Paste the code below to link it to tracking popup that you can also see in backend Shipments area.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
if($_order->hasShipments()) {
    $show = true;
    foreach($_order->getTracksCollection() as $k=>$v) {                            
        if($v['carrier_code'] == 'custom' || $v['carrier_code'] == '')
            $show = false;
    }
    if($show) {?>
        <span class="separator2">&nbsp;</span>

        <a class="askTrackBtn" href="javascript:;" onclick="popWin('<?php echo Mage::helper('shipping/data')->getTrackingPopUpUrlByOrderId($_order->getId()) ?>', 'tracking the order', 'scrollbars=yes,width=800,height=600,resizable=yes');return false;"><span><?php echo $this->__('Track Order') ?></span></a>
       <?php 
    }
}?>

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications