Tag Archive - way

Make A Custom Function Available In The Entire Magento System

One of my co-workers recently started working with Magento. Until recently he was primarily working in a custom system we had developed.  One of the functions that he was very used to using was a debug function that simply was a wrapper for PHP’s print_r() that echoed out the beginning and ending pre tags.

Now this was not a show stopped for him, but it quickly became irritating for him to not have quick function available to him in Magento (especially since Magento has some massive objects) and he forgot to put in his pre tags.

Since I am a nice guy, I decided to see if there was a way that I could bring this little function into the system for him and it was easier than I thought it would have been.

In order to add a function that is available in every part of the Magento system you have to remember how a request flows through Magento. Everything starts in the index.php file. All I had to do was add this function to this file and it was available everywhere.

Now a few things to consider when doing this.

  • I would not do this in production (it probably would not hurt in this case, but you never know)
  • In terms of Magento coding standards, I would call this a little “dirty” the more “correct” way to do this would be to put this code somewhere in a header in a module.

If you are curious what the actual function looked like I have posted it below:

function print_array($var, $echo=false) {
  echo "<pre>";
  $str = print_r($var, $echo)
  echo "</pre>";
  return $str;
}

So there you have it. A quick and dirty way to bring in your own functions to the entire Magento system.

An Overview of Magento’s Get and Set Methods

Magento handles its get and set methods in a way that if you do not understand how it works it can be confusing to you. In the beginning of this article you have to take a leap of faith and believe in magic.

In Magento you can take nearly any model and get all the information about it with a simple function call. See the example below:

$product = Mage::getModel('catalog/product')->load(1500);
print_r($product->debug());

This code will return an array of all the basic information about the product. For this example lets say that it only returned two elements to the array “name” and “meta_description”.

Now in order to get this information you will have to take the information and camel case it to create the function. So to get the information you would write the following:

echo $product->getName();
echo $product->getMetaDescription();

Do you see how the Meta Description function was formatted? The first letter of the words are capitalized (or camelized) to create the function. So lets say you had an attribute called this_really_important_long_attribute you would call $product->getThisReallyImportantLongAttribute()

Now that you understand that, in order to set the attributes value, you simply replace “get” with “set” and pass the variable as a parameter of the function. So to set the name you would make the following function call:

$product->setName($new_name);

Ok remember when I said that you had to believe in magic? That is because if you look at the Product model, you will not find these functions defined anywhere. In fact if you are hardcore you can follow the class all the way to the top and you will not find these functions defined.

Ok now that you believe in magic, let’s take the curtain away and show you how Magneto accomplishes this. Nearly every class in Magento extends from the Varien Object (which is located in the /lib/Varien/Object.php file). You will see there is a getData() and setData() functions. These look like they could do the actual setting and getting but this is not the magic we are looking for.

Farther down in the file you are going to find the __call() function. This is the magic. This magic here is actually from PHP. The __call() function gets called anytime you call a function on a class that does not exist. The __call() function takes two parameters. The first one is the name of function that was called, the second one is the data that was passed to the function.

Now what Magento does is take the function that was called, determine if the function is a get or set function. Then it parses the string to find the attribute and then it makes the appropriate function call to get or set the data.

Once you see how this works you can see how awesome this functionality actually is.

Get Your Magento System’s Time

Magento handles time in a pretty different way. Instead of just using the time on the server it automatically converts the time to the selected time zone in Magento’s system.

The way to get the current time is by using the following code:

Mage::getModel('core/date')->timestamp(time());

What this will do is take the timestamp that you get from the time() function and converts it to the timestamp that Magento says what time it is. So you can wrap this function into PHP standard date() function to output in a format you are used to.

Using jQuery Listnav Plugin

Yesterday I was looking around for a creative way to display what will eventually become a very large list of information. Generally I would just use extra links and sort them alphabetically. For me it had never seen like the most ideal solution. Forcing the user to wait while the page reloads itself is actually a bad solution. What if you could just have all the information load once and the user could then instantly browse all that information?

This is how I came across the jQuery ListNav Plugin by iHwy. It does exactly what I was looking for. It takes a large list of information and makes it very easy to work with.

All you have to do is place the location you want the navigation bar to go, then you create your unordered list, and then just put in a single jQuery command and it does the rest. It could not be simplier.

I have so many different ideas where this could be used. The one I probably see is creating an interactive product listing for an eCommerce site. Many websites make HTML versions of their sitemaps for SEO rankings. These pages generally have little to no use for the end customer because it usually is just a bunch of links displayed. With this plugin, you really could make this information useable by customers. A very neat concept.

You can view the demo of the plugin in action here. You can also view how I used this on another one of my websites by clicking here.

The Ultimate PHP CSV Parser

Lately I have been trying to find cool PHP utilities that I can use to handle my day to day tasks. Lately parsing CSV files in PHP has been something I have had to do over and over again. Such a basic task can be such a pain considering all of the different formats that they can be in (mostly the quotes around some of the values). I would do find and replace on the file to remove all the extra stuff and hope that I did not corrupt the data.

There had to be a better way to do this. I did some searching and I found Ming Hong Ng’s CSV Parsing class. It worked perfectly. I have parsed a ton of CSV files with no problems at all. You can download this class from: http://minghong.blogspot.com/2006/07/csv-parser-for-php.html

It is so simple to use too! Simply create an instance and call ParseFromFile or ParseFromString. The data is stored in the data instance variable. So simple!

This is something that should be apart of any PHP programmers toolkit.

Page 1 of 212»