Shopping cart & basket class

200 views
Skip to first unread message

Andrew Brookes

unread,
May 7, 2015, 5:22:09 PM5/7/15
to f3-fra...@googlegroups.com

update, shop is at  http://www.ginbrookesfoundation.org/shop


I would like to be able at some time in the future to be able to implement a shopping cart system since for an NGO that i’m involved with (web in construction using f3 http://www.ginbrookesfoundation.org) ,  one way of raising funds is buy selling items maybe some some cultural physical items or maybe just digital downloads.


So I have started to read up and take in the basic concepts which seems to involve sessions and items stored in a MySQl database with say a table of products of Id Primary key, auto increment , a varchar field containing src path to image to display and product details etc. Then another table containing temporary items that have been selected for the cart .


Now I see there is the basket class and i understand beneath it high quality code but basically they are snipets of code which you IT guys can put together as required; whereas I as a none IT guy really need to be able to cross reference the code with actual examples.


I can understand that if a product is displayed and clicking on the product image shows a button “add to cart” there is hidden input details; its just that even though I have written my own PHP classes I can not at the moment make the mental leap to see how I can actually use and manipulate data from MySQL tables using the basket class. Does anyone have example code they can share or can elucidate a little on how to use the code?

ikkez

unread,
May 8, 2015, 1:37:08 AM5/8/15
to f3-fra...@googlegroups.com
Hi Andrew.

In one of my project, some website guests are able to bookmark things. Basically a set of user bookmarks is similar to a shopping cart. If a guest decide to register as a real member, I use the Basket to save the bookmarks to the database, otherwise his/her bookmarks are gone when the session expires. I have simplified some code for you:
http://pastebin.com/e36TKNfW
When a guest adds a personal bookmark (shopping cart item), I just store the ID of the item they want bookmarked in the basket. This ID is the autoincrement primary key of my items-table in the database. When I use my Bookmarks->get function, it uses the stored IDs in the session-basket to find and return the real full Item records from the database. That's it. So in essence I'm just saving a list of IDs in the session and use those to compose my query to get the list of items they have chosen. When they register, I copy all Basket items to a new sql table (user,item_id) and use this to hydrate my Basket everytime. Maybe there are simpler ways, and my code it not fully optimized, but it worked for me. And if you have a solution that works for your case, then it's totally okay too.

I hope this was helpful :) have a nice day.

Andrew Brookes

unread,
May 9, 2015, 6:10:06 AM5/9/15
to f3-fra...@googlegroups.com

Hi Ikkez

   power back here after nearly 2 days off!

   Downloaded the code   and had a quick look

  looks good its much clearer  with a class that can be instantiated, protected variables declared
  and functions    set out and ready to be used via  OOP

  will study it!

  Many thanks

  Andy

  
 

Andrew Brookes

unread,
Jun 14, 2015, 11:52:56 AM6/14/15
to f3-fra...@googlegroups.com


In the end i decided to give it a go my way; at least i ,might learn more PHP (mostly by trial & error) along the way.
   Basically i managed to do a simple system of select , show items in cart, and remove any single  item from cart all  
   based along    SESSION
    eg   $f3->set("SESSION.item[$itemFromPost]",$itemFromPost); i just set primary key (id) to array position so i know
    if a product has id of 4 then to clear it its: $f3->clear ("SESSION.item[$itemId]"); by the way single quotes don't work
    as solved by xfr35 in stack overflow

   i have one table of items with id (auto increment), description, price, image path
   i just have a button under each item ( hidden field of id  with a POST to a route) i wouldn't mind someone looking over
   the    code . theres 4 routes & a couple of views so far- too much to post? whats the best way of presenting code? attach a file?
 
 

xfra35

unread,
Jun 14, 2015, 1:50:35 PM6/14/15
to f3-fra...@googlegroups.com
Hey Andrew, you can paste code here.

Unlike single quotes, double quoted strings are interpreted by PHP, so if $itemFromPost equals 4:

"SESSION.item[$itemFromPost]" returns 'SESSION.item[4]'

while

'SESSION.item[$itemFromPost]' remains 'SESSION.item[$itemFromPost]'


Andrew Brookes

unread,
Jun 15, 2015, 2:59:16 PM6/15/15
to f3-fra...@googlegroups.com


   I have loaded stuff on basic cart to web , so anyone can check it  out url which is not public is:

   http://www.ginbrookesfoundation.org/shop

   Code (a bit wooly) is here :       http://pastebin.com/BdvB1j8F
  
    Doing this is partly an exercise but i would like to know if this is feasible to use? by continuing with it!
    If you have a look bear 2 things in mind :
  1) i am not an IT professional ( so put your empathy cap on!)
  2) This is going to help an NGO in Ghana where to give kids a  chance of education
  

xfra35

unread,
Jun 15, 2015, 5:54:12 PM6/15/15
to f3-fra...@googlegroups.com
Hey Andrew,

I think you're on the right track :P

A few tips:

- You don't need to redeclare the db connection in each controller. Just store it in a variable and reuse it when you need.

- It's a good habit to parameterize queries when dealing with unsafe input (like POST). So instead of:
$db->exec("SELECT id, description,image ,price   FROM items  where id= '$itemFromPost'  ");
you could use:
$db->exec('SELECT id, description,image ,price   FROM items  where id=?', $itemFromPost);


Andrew Brookes

unread,
Jun 16, 2015, 11:18:44 AM6/16/15
to f3-fra...@googlegroups.com


  xfra35

  Thats a big help! because i use xampp for development & then loading  to the web which obviously has different db connection   details,   in my index.php i had at one point about 14 pairs of lines in different routes  which i had to // out as i switched from xampp to loading to web. Now its just one single to un // and one // at the top of the page.

 My code has disappeared from pastbin ! luckily you saw it before it vanished !  

cheers

Andrew Brookes

unread,
Jun 18, 2015, 11:09:57 AM6/18/15
to f3-fra...@googlegroups.com

  just want to put an update for anyone else trying PayPal REst API PHP SDK

  paypal examples help a lot & sandbox using test facilitator & test buyer work great- the big issue i had was going from Sanbox to live Pay Pal . At https://developer.paypal.com whats well documented is to use live id and secret credentials and also to use "live"  instead of "sandbox" in  
    $sdkConfig = array(
    "mode" => "live",
    "http.ConnectionTimeOut"=>30,
    "log.LogEnables"=>true,
    "log.Filename"=>"../PayPal.log",
    "log.Loglevel" =>"FINE");
    
what i couldn't find was why i still got a 401 error and api.sanbox in the error

The answer is : I've run a test on your site and I can see that you're using the SDK 1.4.0. Would it be possible for you to update to the latest version 1.5.0?

If you don't want to deal with updating the SDK, I can suggest a workaround. You'll find a file called PayPalConstants.php in lib/PayPal/Core folder. You can simply edit the Sandbox endpoint constant from const REST_SANDBOX_ENDPOINT = "https://api.sandbox.paypal.com/"; to const REST_SANDBOX_ENDPOINT = "https://api.paypal.com/";
Reply all
Reply to author
Forward
0 new messages