Using Custom PHP Codes on Magento
filed in Magento, Parkyeri, Programming, Web - Internet on Sep.05, 2008
Magento is one of the newest and awarded ecommerce application right now. And I seem to have a project launched nowadays about it.
Above the ecommerce part like adding/modifying products, it also has a built-in CMS. It looks quite useful. Not very powerful though. It just evaluates html code and not php. In most of the time this is prefered by the way. Because of security risks and things like that. But there are also cases which you want to do if you are importing something to magento or if you are just wanting to do in a quick and dirty way!
Well, the workarounds you find are not quite good. Simply they just take the CMS code, write it to a temporary file and include it, or writing a bit complicated parser inspired from joomla/mambo. Check out here for the details of the workaround. The tutorial I’m putting here is from there to, however I will also try to write solutions to problems that I have encountered (mostly because of magento versions).
This tutorial is working for magento version 1.1.3. You can download the latest version from here.
Now open up your favorite php/xml/html editor. While it will open up, also open your magento installation folder. Magento has a lots of folders for lots of things, which I do not know why. The code part is in app/ directory as you may guess. In the app folder you will find the etc/ where there are configurations for magento generally. Our PHP Code will be a module to the Magento, so we have to go modules directory and create a new xml file, named according to your module. The syntax is like: Parkyeri_customPHP. The first part till underscore means the name of the module, int this case Parkyeri. The second part indicates the component of the module. So you may have different components in one module, in this case customPHP, If we are talking about custom codded PHP pages, so in this module named Parkyeri, there can be different pages like: About, Contact, Jobs. And these are all different components, doing different things. If we were trying to create the About page then we had to use something like Parkyeri_About. You can add all of them in the same xml file.
Open up /app/etc/modules/ and create a file named Parkyeri.xml (the name of the module) and add these lines to file:
<?xml version=“1.0″?>
<config>
<modules>
<Parkyeri_customPHP>
<active>true</active>
<codePool>local</codePool>
</Parkyeri_customPHP>
</modules>
</config> But what this means? You declare a new module named Parkyeri to the global site by naming the file Parkyeri.xml. Then you define in the module config file that, this module has a component named customPHP. You say that this component is active and the system can find it in the app/code/local directory by looking at the codepool
As you get it now we pass to the next stage. Now open up app/code/local/Parkyeri/customPHP/etc/ and create a file named config.xml (the default configuration file name for every module.) and add these lines in it.
<?xml version=“1.0″?>
<config>
<global>
<blocks>
<parkyeri_customphp>
<class>Parkyeri_customPHP_Block</class>
</parkyeri_customphp>
</blocks>
</global>
</config> What does it mean? This is more of a mapping file than a configuration file. What you are doing here is to map (you may also think bind) a class named Parkyeri_customPHP_Block to a block called parkyeri_customphp. So whenever a block of type parkyeri_customphp will be called, this block will look out for the class you defined in this configuration/mapping/binding file. But also you are defining the location of the component files that you want to call. In most of the cases, you want to call a component’s block from a module. So when calling you will say that I want a component named customPHP, and a block named test and this component is under the module Parkyeri. We move on…
Now the fun part, open up app/code/local/Parkyeri/customPHP/Block and create a file named Test.php and add these lines in it:
<?php
class Parkyeri_customPHP_Block_Test extends Mage_Core_Block_Abstract
{
protected function _toHtml()
{
// put here your custom PHP code with output in $html;
// use arguments like $this->getMyParam1() , $this->getAnotherParam() $html = “Hello” . $this->getWorld();return $html;
}
} So, we have finally reached the part which you are most comfortable, writing code! You will write down all the code you want to evaluate in this file. The _toHtml() method is called when you call it in the cms part. A magical function just like toString(). As you may remark the name of the class, is conventional according to the location of the class. That was how the previous configuration knew where to look when mapping.
And the final part is to embed it in the CMS, this is easy part. But the part where you get dissapointed most. Add this to page you want to show your custom php code:
{{block type=”parkyeri_customphp/test” world=”World”}}
So, it’s obvious isn’t it? In this code part, you say that, you want to put a block here. The type of this block is parkyeri_customphp/test. So actually you want a block named test under the customphp component which also a part of parkyeri module. But you may be curious of the rest, on what I mean by saying, world. I’m sending a parameter to the class. Remember the code where there was $this->getWorld(). This world is that world!. So the code will print out “Hello World” (without an exclamation).
So this was it! Did it worked? No I didn’t did it? The real reason that it did not worked, is because, you did not refresh the caching system!! Yes, it’s that simple! It gave me a lots of headache though, hope it won’t to you. You can disable the caching from System -> Cache Management by the way. Better then refreshing every time ![]()
If you are stuck on somewhere you should really check out magento forums. There are really helpful people out there from real developers, ceos and community experts.
September 6th, 2008 on 3:23 am
Hi,
I want to register a session in magento. How can I do it?
Regards,
Ranjan
September 12th, 2008 on 3:51 am
I’m not totally sure If I get what you mean.
So if you mean that how you could set a session variable in php, you may use the codes above to create a custom page like that. And in the _toHTML() function you can always set a session variable $_SESSION['session'] like this. But I don’t think that’s what you are talking about.
I really have no idea. Maybe Magento forums will be more helpful than me. There are lots of people who are really helpful out there.
Roy
September 16th, 2008 on 9:03 am
[...] I had mentioned in my previous post Magento is an open source ecommerce application made with PHP and Zend Framework. Company behind it [...]
September 16th, 2008 on 10:10 am
Thanks! Do you have any thoughts/opinions/advise on code management and deployment for magento?
September 17th, 2008 on 5:47 pm
@david
It really looks easy to customize magento code for yourself. The problem is first to understand how the code works. How the configuration files are used, what blocks are around, what are the files which gives the output. It’s a bit complicated.
There are lots of database tables, it’s not easy to see the relations between them and other kind of things. The reason that its hard for me is that I’m a stranger to the core code of Magento. I’m sure that it will get easier with each time I do something on it.
The best way to understand is through making something. I suggest that you try to customize your magento. Like changing the html layout or other kind of simple things.
When you get an understanding of how the core code and work flow works, it’s really easy to customize it. (Which I did not understood it very well yet.) You can always extend the core object and derive your own class. Then you bind your newly created class instead of the core class and this is it! Your customized class is used in all the places where the core class was used. So this way you can always update magento without having fear to lose your own code. I guess that’s the part where magento is powerful. Because in all most of the open source project there is no such thing. I used a heavily modified phpbb portal for 2 years, I was trying to develop it, but the phpbb team was so good that all the free time I had was passing in updating to the new phpbb
Above that, it really gives you everything you need to create an online ecommerce application. The only missing part is the auction but as community grows and does understand better the core concepts, it will be easier to write it I suppose I had an intention to make it but I don’t know have enough of the understanding yet.
I hope I was able to help.
Roy
October 6th, 2008 on 10:14 am
Would appreciate your help in understanding where exactly do I need to put the code {{block type=”parkyeri_customphp/test” world=”World”}}.
am not able to see any output by doing the following.
I did put the following code in the content part of one of the CMS page :
HELLO RUCHI …………………..
{{block type=”parkyeri_customphp/test” world=”World”}}
HELLO AGAIN !!
Your help is needed, Thanks in advance. Please HELP.
October 6th, 2008 on 10:58 am
@ruchi
You add this block part to the textarea that comes when you edit the cms page.
Perhaps there is a problem with system cache thing. It had passed days before I finally figured out it was because of the caching.
Be sure that the type=”" part is correct. Be sure that your module name is parkyeri and component name customphp. And under customphp folder there is block folder and under it a test.php file which contains your php code. I’m not very sure but it must be case sensitive. You must check the cases you have written in the xml configs.
hope that helps.
Roy
November 5th, 2008 on 12:42 am
sir,
i had done all the steps that you said.But i got the following error.
Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 1: parser error : XML declaration allowed only at the start of the document in C:\wamp\www\magento\lib\Varien\Simplexml\Config.php on line 501
Trace:
#0 [internal function]: mageCoreErrorHandler(2, ’simplexml_load_…’, ‘C:\wamp\www\mag…’, 501, Array)
#1 C:\wamp\www\magento\lib\Varien\Simplexml\Config.php(501): simplexml_load_string(’ loadString(’ loadFile(’C:\wamp\www\mag…’)
#4 C:\wamp\www\magento\app\code\core\Mage\Core\Model\Config.php(171): Mage_Core_Model_Config->_loadDeclaredModules(Object(Mage_Core_Model_Config_Base))
#5 C:\wamp\www\magento\app\code\core\Mage\Core\Model\App.php(236): Mage_Core_Model_Config->init(Array)
#6 C:\wamp\www\magento\app\Mage.php(424): Mage_Core_Model_App->init(”, ’store’, Array)
#7 C:\wamp\www\magento\app\Mage.php(443): Mage::app(”, ’store’, Array)
#8 C:\wamp\www\magento\index.php(52): Mage::run()
#9 {main}
Please give a solution to this problem
November 7th, 2008 on 8:07 am
@manu
This might be a problem about your xml file. Some missing “>” or “<”, maybe an unclosed tag. Please check that your config.xml and Parkyeri.xml (or the name of the module you are creating) sytax is valid. An xml editor might be useful to find syntax problems too.
November 10th, 2008 on 7:35 pm
Hello, I am in need of some advice. With Magento what is the best way (request variable php?) to provide a custom url (for embedded discount code for banner ad (ie click here and receive 10% off)? Right now I utilize the promotion codes/discount box in the shopping cart but I want to be able to create customized urls.
thanks for any guidance, Matt
November 11th, 2008 on 12:35 am
sir,i had solved it…thanks..
There is one more problem.I have to add some products to my website.When adding products, the images are not able to add with this new products. Please help immediateley.Please give a detailed solution to this problem.
manu
November 11th, 2008 on 4:31 am
@mstanchi
Even I’m still not an expert, perhaps the link you are giving can set a predefined promotion code on backend. I’m not very sure how you can do this as I’m not familiar with the promotion part. But the best way looks like is the condition part of the promotion rules. Something like “if a link visited” matches your request I guess. But how you can do it, I don’t know. I guess you have to look on magento’s forums and wikis.
@manu
Again it might be possible that the folder which the files are uploaded cannot be written (chmod problems). Or perhaps there is a bug with your current magento installation(I was unable to open browse files pane with magento 1.1.6 and don’t know why yet.)
Can you be a bit more specific? Once you click on browse, and add files, you have to upload them. Perhaps you have missed it. I had missed that
November 25th, 2008 on 2:46 pm
This is great work got it all working, But how would you add more than one component to a cms page though? thansk.
November 25th, 2008 on 4:46 pm
Defining another component to the same module is similar.
You had used the and xml file defining your module (/app/etc/modules/Parkyeri.xml in this case.) All you have to do is define another part to this module just like you have added parkyeri_customphp.
Let’s say if your component’s name will be “myphp” than you should add someting like this to Parkyeri.xml below “</Parkyeri_CustomPHP>”:
<Parkyeri_myphp>
<active>true</active>
<codePool>local</codePool>
</Parkyeri_myphp>
It won’t be enough of course you should also add additional entryies to the /app/code/local/Parkyeri/myphp. Configuration files and block files. You should go on just like you did for the customphp part.
Hope that this helps
November 26th, 2008 on 10:42 am
Hello,
I am trying to use that module to add some personnal information in a CMS page. It seems to work perfectly, but i have problems to reach the user firstname.
I used that code, but it doesn’t seem to work…
$html = “Hello” . $this->getCustomer(’firstname’);
Any idea?
November 26th, 2008 on 10:49 am
Hi,
When you are calling your module in the cms page you should use somehing like this:
{{block type=”parkyeri_customphp/test” world=”World”}}
While you want to get that “world” parameter, you are using getWorld(). I don’t know if you can send arguments to that functions but if you are using something like this:
{{block type=”parkyeri_customphp/test” firstname=”Roy”}}
then you have to call:
I don’t think if the below code works, but it does not really make sense
{{block type=”parkyeri_customphp/test” customer/firstname=”Roy”}}
Hope that this helps.