3

Im sure the title sound a bit complex or lame. Im trying to develop a component that will keep the count of users that Added a product in the cart but didnt finish with a checkout ( canceled the order before paying ) .

I created a new table in the db and in the controller of virtuemart i added this code: components/com_virtuemart/controllers/cart.php

function add() {
        $tmp = JFactory::getUser();
        $db= JFactory::getDbo();
        $ready = $db->getQuery(true);
        $query= "INSERT INTO #__nobuyers (id,name,email) VALUES ( '". $tmp->id ."','". $tmp->username ."','". $tmp->email ."') ";
        $db->setQuery($query);
        $db->execute();   
        /*
        $session =& JFactory::getSession();
        $session->set( 'cartcheck', 'cartisnotempybro' );
        $GLOBALS['yoyo']=notempty;
        */





public function confirm() {
        $tmp = JFactory::getUser();
        $db= JFactory::getDbo();
        $ready = $db->getQuery(true);
        $query= "DELETE FROM #__nobuyers where ID=". $tmp->id;
        $db->setQuery($query);
        $db->execute();  

SO when a Registered and Loged user will add a product in his cart i keep his info at my db. If he checkout i remove him. This is working fine. The problem is when a user first add a product to the cart and logs in after. So what im trying to do is : When a user add a product in a cart, keep a global flag variable. After that edit the com_users/controllers/user.php login() function so when someone try to login check if the flag is set ( someone added a product in the cart ) and if so, add him in database right after log in.

Something like that :

$session = JFactory::getSession();
$seccode=$session->get('cartcheck');
if ($seccode) {
or
if (!empty($GLOBAL['yoyoyo'])) { //do stuff

ALtough i try to access a variable setted from com_virtuemart inside the com_users.The var_dumps i used returns NULL. Is it possible ? And if yes how we achieve that ?

3

Beyond the fact that your "global" variable is changing ($GLOBALS['yoyo'] is not at all the same as $GLOBAL['yoyoyo']), all variables except the session (which you also appear to be using) will not persist between page loads.

So you are correct in trying to use the session. However, the session will be reset to a degree after the user logs in. You would likely have to add some code before login to check if the value exists and store it in another variable, and then after login check that variable to actually update the db.

Another solution, that I think would be simpler, is to not save the data at all on your end and just write a user plugin that checks for onUserLogin. Within that function, if the user has successfully logged in, check if their cart has any items in it. If so, add them to your database.

References for you:

Creating a plugin: https://docs.joomla.org/Creating_a_Plugin_for_Joomla

Are there items in the cart: https://stackoverflow.com/questions/13929564/how-to-get-all-products-in-cart-in-virtuemart

  • The "yo" i missed was a typo, in my code its the same. Because im not a joomla expert please correct me if im wrong: I need to create plugin for the the onUserLogin event, and create a module also to cheack if the user has any product inside his cart like that link you pasted say? or add that code in the plugin also ? – IseNgaRt Jan 29 '15 at 18:54
  • I would just do it all in the plugin. The logic is "check if a user is logging in. if they did just log in, do they have products in their cart? If yes, add them to the custom table." Shouldn't be too much code overall. – David Fritsch Jan 29 '15 at 18:57
  • Also if i use the first way you suggest: I should save the session in a new var inside the function of com_user and check it after use logged in. ALtough i var_dumped the session if i remember well in the start of function( Before the log in event start ) and it was NULL. Is it normal ? And if i wanted to store it inside a cookie that should happen before i include any html code so the cookie would be stored in the http header if i wanted it to be global, right ? Thanks for the feedback. – IseNgaRt Jan 29 '15 at 19:00
  • Actually it worked with changing setting the value of Session in a different var ! – IseNgaRt Jan 30 '15 at 7:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.