Magento Useful Template Snippets

Create a block object inside a phtml template file:

<?php
$myBlockObject = $this->getLayout()->getBlock(“my_block_name”);
echo $myBlockObject->blockFunction();
?>

Insert a phtml template file into another phtml file

<?php echo $this->getLayout()->createBlock(‘core/template’)->setTemplate(‘page/html/someother.phtml’)->toHtml(); ?>

Get current product from registry

<?php
if( Mage::registry(‘current_product’) ){
$product = Mage::registry(‘current_product’);
}
?>

Insert static block into phtml file

<?php echo $this->getLayout()->createBlock(‘cms/block’)->setBlockId(‘block_identifier’)->toHtml();
?>

Get request (querystring) parameter

<?php
$myVar = Mage::app()->getRequest()->getParam(“myVar”);
?>

Get controller name
Can use this to check whether you are on a product or a category page

<?php
if($this->getRequest()->getControllerName()==’product’){
if(Mage::registry(‘current_product’)){
//do something useful
}
}
?>

Facebook Comments

mySQL (mariaDb) Useful User Management Via Command Line

Here is a collection of useful and common mysql commands for managing database users using the command line, perhaps whilst logged in to the server via ssh. The following commands work for MySql and mariaDb. Don’t forget to include the semi-colon at the ends of each command. Continue reading mySQL (mariaDb) Useful User Management Via Command Line

Facebook Comments

Magento Cookies not Working in Chrome or Opera on Frontend

So, I was testing the “this site is using cookies” functionality in Magento. It worked fine on a local environment but failed to work on a test “live” IP address site. I say failed, but it worked fine in IE and Firefox whereas in Chrome hitting the “I agree to cookies” button didn’t set a cookie and redirected. I eventually solved it. Continue reading Magento Cookies not Working in Chrome or Opera on Frontend

Facebook Comments

Magento: Passing a value from a controller into the phtml

Another Magento quickie for when you want to pass a value into your phtml template file that is set/generated inside the controller.

Inside the controller you can do this:

$block->assign(array(“myVarName”=>”value”));

Where $block could be something like

$block = $this->getLayout()->getBlock(“head”) ;

if you wanted to pass the value into the head template.

You can also set a value like this:

$block->setData(“myVarName”,”value”)

Then, inside the phtml template file you can get the value like this

$this->myVarName

Note you can pass multiple variables through a single $block->assign() like this

$block->assign(array(
“myVarName”=>”value”,
“anotherVar”=>123,
“enoughVars”=>true
));

Facebook Comments

Saving the page HTML in Chrome without reloading

This chestnut can drive developer crazy.  In Google Chrome hitting “save as” or “view source” will reload the page.  On pages where sessions or cookies are cleared (shop checkouts for example), form data is submitted, or the like, then the reloaded source may no-longer be the same as the page you were just looking at.  Argh. Here is my solution to the issue. Continue reading Saving the page HTML in Chrome without reloading

Facebook Comments

Chrome ‘internal’ URLS

As a web developer I use Chrome a lot.  The dev tools are good and, in general, it is a good browser.  Sometimes, though, I find some odd behaviour like the browser has cached some DNS settings that I have since changed, or I want to switch the experimental features on or off for some testing.  Here is how you get to access those settings and then the page to see all the available option/settings URLs.

Continue reading Chrome ‘internal’ URLS

Facebook Comments

Applying Magento SUPEE Patches on a Windows environment

Ok, for reasons of general simplicity and familiarity I like to develop my php applications in a Windows environment using WAMP.  However, this can be problematic when needing to apply SUPEE patches to development instances of Magento projects.

Continue reading Applying Magento SUPEE Patches on a Windows environment

Facebook Comments

Red Hat / Centos: Web services not starting up on a server reboot

Ok, so your webserver has crashed. You reboot it and…and…nothing. No website.  Your apache / httpd and mysqld services have not restarted along with the server.  Here is how to check if they should and make them do so if they aren’t.

Continue reading Red Hat / Centos: Web services not starting up on a server reboot

Facebook Comments