Is It Possible To Integrate CakePHP and WordPress WooCommerce?

Absolutely! WooCommerce is a plugin built for the WordPress CMS (Content Management System). However, a client had an existing website built on the CakePHP framework, so we wanted to only take advantage of the e-commerce power of WooCommerce. Since we needed WordPress to power WooCommerce, this tutorial will provide instructions on WordPress core files to modify to get this working, and not necessarily WooCommerce. These instructions could be very useful if you’re trying to get a WordPress blog running with CakePHP. I have seen code out there to put the folder into the /app/webroot directory of CakePHP but seemed to give unnecessary headaches with htaccess rewrites and so on. I decided to put it into the root hosting folder along with CakePHP’s app, cake, and vendors folders. I named my folder with all of the WordPress files in it “store”. The CakePHP and WordPress database tables are in the same MySQL database with the same database user. Here are the following things you’ll need to do to get this up and running:

Errors this will fix when you integrate CakePHP and WordPress WooCommerce:

  • Fatal error: Cannot redeclare stripslashes_deep() (previously declared in /cake/basics.php:657) in /wp-includes/formatting.php on line 1721
  • Fatal error: Cannot redeclare __() (previously declared in /cake/basics.php:677) in /wp-includes/l10n.php on line 164
  • Potential error where a bunch of random WordPress strings print out at the top of your content

CakePHP

File: /app/app_controller.php
Location To Place Code: Top of file, above the class AppController extends Controller
Code:

global $wpdb;
define('WP_USE_THEMES', false);
include($_SERVER['DOCUMENT_ROOT']."/{folder_name_wordpress_files_are_in}/wp-config.php");

Notes: You’ll want to replace the third line of code with the location of your wp-config.php file. Mine happens to be at /store/ because I made a root folder of store.

File: /cake/basics.php
Code Line Reference: 685
Old Code: 

echo I18n::translate($singular);

New Code:

return I18n::translate($singular);

Notes: You’ll want to replace the third line of code with the location of your wp-config.php file. Mine happens to be at /store/ because I made a root folder of store.

 

WordPress

File: /store/wp-includes/l10n.php
Code Line Reference: 161
Old Code:

function __( $text, $domain = 'default' ) {
return translate( $text, $domain );
}

New Code:

if (!function_exists('__')) {
function __( $text, $domain = 'default' ) {
return translate( $text, $domain );
}
}

Notes: You need to replace the code around line 161 that defines the translation function. This is because CakePHP also defines a function to translate strings, so it throws you an error telling you its been declared already.

File: /store/wp-includes/formatting.php
Code Line Reference: 1708
Old Code:

function stripslashes_deep($value) {
 if ( is_array($value) ) {
 $value = array_map('stripslashes_deep', $value);
 } elseif ( is_object($value) ) {
 $vars = get_object_vars( $value );
 foreach ($vars as $key=>$data) {
 $value->{$key} = stripslashes_deep( $data );
 }
 } elseif ( is_string( $value ) ) {
 $value = stripslashes($value);
 }

 return $value;
}

New Code:

if (!function_exists('stripslashes_deep')){
function stripslashes_deep($value) {
 if ( is_array($value) ) {
 $value = array_map('stripslashes_deep', $value);
 } elseif ( is_object($value) ) {
 $vars = get_object_vars( $value );
 foreach ($vars as $key=>$data) {
 $value->{$key} = stripslashes_deep( $data );
 }
 } elseif ( is_string( $value ) ) {
 $value = stripslashes($value);
 }

 return $value;
}
}

Notes: You need to replace the code around line 1708 that defines a stripslashes function. This is because CakePHP also defines a function to stripslashes with the same name, so it throws you an error telling you its been declared already.

Share

Comments

  1. By Konstantin

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Share