Wednesday, June 8

How to Use Magento Session with in Wordpress

My current setup is still the same with the rest of my post here having ‘htdocs’ as my root directory, magento has its own subdirectory ‘htdocs/magento’ as well as wordpress in ‘htdocs/wordpress’. The goal here is to able to use Magento as if it is a native function within our WordPress installation.

Since there is an existing function collision between Magento and WordPress because both application has an existing translator function named __(), our first task is to automatically detect if the function already exists and disable it in Magento and run as usual if it doesn’t.

To do that, locate the file below in your Magento installation:
Copy the file functions.php below from your Magento core folder

path:to-your-htdocs/magento/app/code/core/Mage/Core/functions.php

and paste it in the Magento local folder which can be found below and open it for editing (create needed folders if it doesn’t exists):

path:to-your-htdocs/magento/app/code/local/Mage/Core/functions.php

Locate the function __() or go to line 93:

function __()
{
    return Mage::app()->getTranslator()->translate(func_get_args());
}

replace it with this:
if (!function_exists('__')) {
 function __()
 {
  return Mage::app()->getTranslator()->translate(func_get_args());
 }
}
Why did I choose to disable Magento’s translator function instead of WordPress’? It is because in Magento, it has already been marked as deprecated in version 1.3 and searching throughout the installation I didn’t see any file that uses that function.

Now that the function collision has been solved, let’s proceed in modifying WordPress to include our Mage.php file. Locate and open the WordPress file below:

path-to-your-root-htdocs/wordpress/wp-includes/functions.php

Scroll down to the end of the file. Add the codes below right after the last function statement.

function magento($name = "frontend") {
 // Include Magento application
 require_once ( "../magento/app/Mage.php" );
 umask(0);
 // Initialize Magento
 Mage::app("default");
 return Mage::getSingleton("core/session", array("name" => $name));
}

That’s it! I know it’s weird that it isn’t like the solution given by others that require modifying a lot of files. Nevertheless, I tested it and it does work. You just have to make sure that whenever you use the magento() function to any file within WordPress, always place it at the top most part of the code where there’s no header request or else you will get a similar error below:

Fatal error: Uncaught exception 'Exception' with message 'Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent .....................

An example of how to use this is to check whether a customer is logged in or not. To do this, open the index.php of WordPress’ default theme which is Twenty Ten 0.7:

path-to-your-root-htdocs/wordpress/wp-content/themes/twentyten/index.php

Just below line 15, add the magento() function. It should look like the codes below:

<?php


 magento(); 

?>

<?php get_header(); ?>

Add the code below marked as Magento’s custom greeting right after the get_header() function of WordPress in the same file:



isLoggedIn()){ $magento_message .= $session->getCustomer()->getData('firstname').' '; $magento_message .= $session->getCustomer()->getData('lastname').'!'; }else{ $magento_message .= "Guest!"; } echo $magento_message; //echo "
"; //print_r($session); ?>

The purpose of the code change above is to display a ‘Welcome [customer name here]‘ when a customer is logged in, and show ‘Welcome Guest’ when they are not.

Hope it Helps.. Thanks...

No comments:

Post a Comment