Add item to basket via AJAX-Call

So, I’m becoming very desperate with this problem. It just won’t work. No error message, nothing. But let me start from the beginning:

A client want’s a special basket layer enabling customers to put items into the basket without leaving or reloading the page. So, for this, I will need AJAX, obviously. I’ve already created the controller for this (I removed every kind of error handling to make sure nothing interferes with the functionality):

class TheModule_AjaxRecipient extends oxView {
      public function addToBasket() {
           ob_start();
           $orderedArticles = json_decode(oxRegistry::getConfig()->getRequestParameter('data', true), true);

           $basket = $this->getSession()->getBasket();

           foreach($orderedArticles as $articleId) {
               $basket->addToBasket($articleId, 1, array(0), null, false, false);
           }
           
           ob_flush();
           
           echo json_encode(array('status' => 1);
      }
 }

The request-data (POST) looks like this:

cl=TheModule_AjaxRecipient&fnc=addToBasket&data={"0":"someRandomOxId","1":"anotherRandomOxId"}&stoken=theSessionToken

The controller fetches the data, foreaches through the containing IDs and tries to add them to the basket via ‘addToBasket()’. And this does work. When I dump the basket object after the foreach loop, all these articles are actually there, and when I dump the result of addToBasket(), it always returns the articleObject it just added. However, all of these articles are not saved, they are not persisted. They are in the object, but disappear right after, like the basket is not saved or … I really don’t know actually. It should work, but it doesn’t. But I also didn’t find another function in the oxbasket.php that would give me the opportunity to add articles into the basket.

This is oxid 4.10.8, running on PHP 5.6 by the way.

Any help is greatly appreciated, because I’m extremely clueless on this one.

Müsstest du den Basket nicht wieder in die Session zurückspeichern? $basket ist ja nur eine lokale Variable, die nach Verlassen der Prozedur verschwindet.

Dear @m431342, could you stick to English in this topic please - even if you know that the thread starter knows another language? It is most confusing for other readers to find German comments in a topic that was originally started in English. Cheers!

well like m431342 said… save the basket into the session :wink:

https://docs.oxid-esales.com/sourcecodedocumentation/4.10.8/oxsession_8php_source.html#l00630

It happened unconsciously, sorry…:cry:

That’s something I thought of too, so I used

$this->getSession()->setBasket($basket);

directly below the foreach. However, that still didn’t save it, so I removed it again because i thought it’s not necessary.

Maybe, because you are using echo you might have “exit” somewhere, then pageClose is not called in oxshopcontrol, and freeze is not called in oxsession.

Oh damn, you’re a genius. That was it, I had an exit right after the echo because I wanted to avoid that oxid tries to read a resource which will throw the error

Smarty error: unable to read resource: "" in ...

I actually don’t want smarty to read a resource here, I just want to return a json. I do understand that oxView probably isn’t the correct thing to use, but I don’t know what else. There are a bunch of ajaxClasses like ‘actions_article_ajax’ or ‘attribute_order_ajax’, but I think that these are for the backend and wouldn’t help me in my situation.

I could probably create a template where I’ll put the json response generated in PHP into and return the template, but that’s kinda dirty and I’d like to avoid that.

Edit/ Well, I just found out I can just use showMessageAndExit() instead, that works too. I’m an idiot.

Thanks a lot for your help, I was struggling a lot with this simple problem.

Yes and the smarty loading probably would cost some time.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.