Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Silverstripe Magento integration [solved]


Go to End


8 Posts   4951 Views

Avatar
SSadmin

Community Member, 90 Posts

29 August 2011 at 10:13am

Hey, guys

Just wonder is there anyone in silverstripe community did a in-depth integration with silverstripe and magento.

i have encounter a weird problem, so any heads up would be helpful.

the folder structure :

+silverstripe root
- shop(magento root)
- test.php

i am trying to bring magento user control to silverstripe page. by calling magento function :

$site_path=realpath(dirname(__FILE__) . '/../..' );

//init magento connection
require_once "$site_path/shop/app/Mage.php";

Mage::app('default');

umask(0);

Mage::getSingleton('core/session', array('name' => 'frontend'));

$session = Mage::getSingleton('customer/session');

var_dump(Mage::getSingleton('customer/session')->isLoggedIn());

$customer_data = Mage::getModel('customer/customer')->$session->id;

//CHECK IF LOGGED IN
if(Mage::getSingleton('customer/session')->isLoggedIn()){

Debug::show( 'Welcome ' .$session = Mage::getSingleton('customer/session')->getCustomer()->getName());

}

It should return true, and aslo retrun the login name from magento.

It works fine on test.php file, but seems not working once you put into the code into Page controller init function. It always return false.

I wonder is some functions in silverstripe RequestHandler conflict with the code or break the session object that passed on by magento.

Any help would be appreciated.

Thanks
Alex

Avatar
MarcusDalgren

Community Member, 288 Posts

29 August 2011 at 10:47am

I haven't tried this myself but my guess is that the reason you're not having any luck is because there's already a session started by silverstripe. In order to get the user session from Magento I think you'd have to close down the silverstripe session first and then start up the magento session to do your stuff.

Avatar
SSadmin

Community Member, 90 Posts

29 August 2011 at 11:25am

thanks for your quick feedback, Smurkas.
I have tried to clean all session before calling magento mage functions.

public function init(){

Session::clear_all();

//docroot
$site_path=realpath(dirname(__FILE__) . '/../..' );

//init magento connection
require_once "$site_path/shop/app/Mage.php";

Mage::app('default');

umask(0);

$session = Mage::getSingleton('customer/session');

var_dump(Mage::getSingleton('customer/session')->isLoggedIn());

$customer_data = Mage::getModel('customer/customer')->$session->id;

Debug::show(var_dump(Session::get_all()));

//CHECK IF LOGGED IN
if(Mage::getSingleton('customer/session')->isLoggedIn()){

Debug::show( 'Welcome ' .$session = Mage::getSingleton('customer/session')->getCustomer()->getName());

}

}

however, it still return flase by some resaon.

well, i have just pick up a interesting stuff by relevant to the problem.

on my standalone test.php (which worked) :

i try to dump the session core

echo var_dump($_SESSION['core']);

it looks like :

array(6) { ["_session_validator_data"]=> array(4) { ["remote_addr"]=> string(9) "127.0.0.1" ["http_via"]=> string(0) "" ["http_x_forwarded_for"]=> string(0) "" ["http_user_agent"]=> string(63) "Mozilla/5.0 (Windows NT 5.1; rv:6.0) Gecko/20100101 Firefox/6.0" } ["session_hosts"]=> array(1) { ["testdomain.dev"]=> bool(true) } ["messages"]=> object(Mage_Core_Model_Message_Collection)#38 (2) { ["_messages:protected"]=> array(0) { } ["_lastAddedMessage:protected"]=> NULL } ["just_voted_poll"]=> bool(false) ["visitor_data"]=> array(19) { [""]=> NULL ["server_addr"]=> int(2130706433) ["remote_addr"]=> int(2130706433) ["http_secure"]=> bool(false) ["http_host"]=> string(15) "testdomain.dev" ["http_user_agent"]=> string(63) "Mozilla/5.0 (Windows NT 5.1; rv:6.0) Gecko/20100101 Firefox/6.0" ["http_accept_language"]=> string(14) "en-gb,en;q=0.5" ["http_accept_charset"]=> string(30) "ISO-8859-1,utf-8;q=0.7,*;q=0.7" ["request_uri"]=> string(14) "/shop/customer" ["session_id"]=> string(26) "a1tdof021stiagvq158m7m1770" ["http_referer"]=> string(0) "" ["first_visit_at"]=> string(19) "2011-08-28 22:58:10" ["is_new_visitor"]=> bool(false) ["last_visit_at"]=> string(19) "2011-08-28 23:12:05" ["visitor_id"]=> string(2) "41" ["last_url_id"]=> string(3) "794" ["do_customer_login"]=> bool(false) ["customer_id"]=> string(1) "1" ["customer_log_id"]=> string(1) "5" } ["last_url"]=> string(46) "http://testdomain.dev/shop/cms/index/noRoute/" }

but if i do the same in silverstripe end inside init();
it returns only part of the obejct. strange @@

array(2) { ["core"]=> array(2) { ["_session_validator_data"]=> array(4) { ["remote_addr"]=> string(9) "127.0.0.1" ["http_via"]=> string(0) "" ["http_x_forwarded_for"]=> string(0) "" ["http_user_agent"]=> string(63) "Mozilla/5.0 (Windows NT 5.1; rv:6.0) Gecko/20100101 Firefox/6.0" } ["session_hosts"]=> array(1) { ["nelsonhoney.dev"]=> bool(true) } } }

Avatar
SSadmin

Community Member, 90 Posts

29 August 2011 at 11:48am

wonder is there a default silverstripe session length limitation on its Session object.

compare the session object ( standalone one and silverstripe ) one at:

https://gist.github.com/1177408

Avatar
MarcusDalgren

Community Member, 288 Posts

29 August 2011 at 11:51am

Session::clear_all(); does not actually kill the session. I think you actually have to destroy it in order to get Magento up and running. Try Session::destroy(false); and if that doesn't work try Session::destroy(true); after calling Session::clear_all()

With the second version you'll actually kill the current session cookie so I'm not sure if the silverstripe session can be retrieved again. I think the best option when working with systems like this is if you have some kind of layer between them and call that with curl or file_get_contents. Since both systems make extensive use of the session I don't know if you can get them running side by side without seriously crippling one or the other.

Avatar
SSadmin

Community Member, 90 Posts

29 August 2011 at 12:35pm

Hey, Smurkas.

thanks for your suggestions. I tried both Session::destroy(false); and Session::destroy(true);. seems still no luck on that.
I do know it could get super annoy trying to make silverstripe integrated with magento, working with magento is not a joy at all..
But thanks for help anyway..
i am doing debuging individually, trying to find the difference between the two.
Let me know if you have any other insight regarding of this.

Cheers.

Avatar
SSadmin

Community Member, 90 Posts

29 August 2011 at 1:52pm

Edited: 23/10/2011 11:51am

Hey, for everyone who may encounter same issue. it has been solved by moving all Mage Initialise code from Page init() to _config.php instead.

so in _config.php add the follow code.

$site_path = realpath(dirname(__FILE__) . '/..' );

// include Magento Application
require_once "$site_path/shop/app/Mage.php";

umask(0); // changes this file's permissions - security issue?

// Initialise Magento
Mage::app('default');
// get the magento session - make sure ttl is the same
// $this->mage_session = $this->get_mage_session();

// You have two options here,
// "frontend" for frontend session or "adminhtml" for admin session
Mage::getSingleton("core/session", array("name" => "frontend"));

Thanks for the help from Barry Keenan.
For the whole tutorial please visit www.gingerleprechaun.com

Avatar
alexmarzec

Community Member, 1 Post

22 July 2017 at 1:53am

I found the following list covers essential Magento functions that will help you take care of basic tasks while writing Magento code. Check this cheat sheet here, https://www.cloudways.com/blog/magento-functions-cheat-sheet/