Need help creating shopping cart

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8398
  • Loc: USA

Post 3+ Months Ago

Right now, I'm trying to create a shopping cart, and that doesn't seem to go very well. What I have so far is...
PHP Code: [ Select ]
<?php
class shoppingCart {
   
    var $product = array();
   
    function add($prod, $quantity)
    {
        $aray = (array)$_SESSION['shopping_cart'];
        foreach($aray as $key => $val)
        {
            if($prod == $key)
            {
                $val += $quantity;
            }
            else
            {
                $key = $prod;
            }
           
            $this->product[$key] = $val;
        }
        $this->save();
    }
   
    function showSaved()
    {  
        return (array) $_SESSION['shopping_cart'];
    }
   
    function save()
    {
        $_SESSION['shopping_cart'] = $this;
    }
}
?>
  1. <?php
  2. class shoppingCart {
  3.    
  4.     var $product = array();
  5.    
  6.     function add($prod, $quantity)
  7.     {
  8.         $aray = (array)$_SESSION['shopping_cart'];
  9.         foreach($aray as $key => $val)
  10.         {
  11.             if($prod == $key)
  12.             {
  13.                 $val += $quantity;
  14.             }
  15.             else
  16.             {
  17.                 $key = $prod;
  18.             }
  19.            
  20.             $this->product[$key] = $val;
  21.         }
  22.         $this->save();
  23.     }
  24.    
  25.     function showSaved()
  26.     {  
  27.         return (array) $_SESSION['shopping_cart'];
  28.     }
  29.    
  30.     function save()
  31.     {
  32.         $_SESSION['shopping_cart'] = $this;
  33.     }
  34. }
  35. ?>
The problem I'm seeing with this is... well, it just doesn't work. I used to have had it at the point where I could just see the first product submitted and that's it, so if I try to buy another product, it wouldn't work, but with this... nothing get's added :x

Thanks for any help in advance :)

[EDIT:] Now, if you submit that for a second trial, you will get the following array:
Code: [ Select ]
Array
(
    [product] => Array
        (
            [1] => __PHP_Incomplete_Class Object
                (
                    [__PHP_Incomplete_Class_Name] => functions
                )
 
        )
 
)
  1. Array
  2. (
  3.     [product] => Array
  4.         (
  5.             [1] => __PHP_Incomplete_Class Object
  6.                 (
  7.                     [__PHP_Incomplete_Class_Name] => functions
  8.                 )
  9.  
  10.         )
  11.  
  12. )
:banghead:

The testing code for it is...
Code: [ Select ]
<?php
    session_start();
    $cart = new shoppingCart();
?>
<p>The Shopping Cart:</p>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<p>Product: <select name="prods">
<option value="1">Pair or Socks</option>
<option value="2">Pair of Underwear</option>
<option value="3">The House on the Hill</option>
<option value="4">Hardy Boys: Collector's Edition Book Set</option>
<option value="5">Juice Boxes</option>
<option value="6">PHP Dev's Book</option>
</select><br />
Quantity: <input type="text" name="qt" size="10" /><br />
<input type="submit" name="submit" value="submit" /></p></form>
<pre>
<?php
if(isset($_POST['submit']))
{
    $cart->add($_POST['prods'], $_POST['qt']);
    print_r($cart->showSaved());
}
?></pre>
  1. <?php
  2.     session_start();
  3.     $cart = new shoppingCart();
  4. ?>
  5. <p>The Shopping Cart:</p>
  6. <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
  7. <p>Product: <select name="prods">
  8. <option value="1">Pair or Socks</option>
  9. <option value="2">Pair of Underwear</option>
  10. <option value="3">The House on the Hill</option>
  11. <option value="4">Hardy Boys: Collector's Edition Book Set</option>
  12. <option value="5">Juice Boxes</option>
  13. <option value="6">PHP Dev's Book</option>
  14. </select><br />
  15. Quantity: <input type="text" name="qt" size="10" /><br />
  16. <input type="submit" name="submit" value="submit" /></p></form>
  17. <pre>
  18. <?php
  19. if(isset($_POST['submit']))
  20. {
  21.     $cart->add($_POST['prods'], $_POST['qt']);
  22.     print_r($cart->showSaved());
  23. }
  24. ?></pre>

Any help would be great.

And if you keep submitting more and more data, the array would grow inwardly... not something like:
Code: [ Select ]
Array
(
    [product] => Array
        (
            [1] = 5
            [4] = 8
            [3] = 1
            [2] = 2
        )
}
  1. Array
  2. (
  3.     [product] => Array
  4.         (
  5.             [1] = 5
  6.             [4] = 8
  7.             [3] = 1
  8.             [2] = 2
  9.         )
  10. }

but it grows something like
Code: [ Select ]
Array
(
    [product] => Array
        (
            [1] => Array
                (
                    [4] => Array
                        (
                            [2] => Array
                                (
                                    [__PHP_Incomplete_Class_Name] => functions
                                )

                        )

                )

        )

)
  1. Array
  2. (
  3.     [product] => Array
  4.         (
  5.             [1] => Array
  6.                 (
  7.                     [4] => Array
  8.                         (
  9.                             [2] => Array
  10.                                 (
  11.                                     [__PHP_Incomplete_Class_Name] => functions
  12.                                 )
  13.                         )
  14.                 )
  15.         )
  16. )
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8398
  • Loc: USA

Post 3+ Months Ago

Alright... I changed the add function to:
PHP Code: [ Select ]
<?php
   function add($prod, $quantity)
   {
      $aray = $_SESSION['shopping_cart'];
      foreach($aray as $pid => $qt)
      {
         if($prod == $pid)
         {
            $qt += $quantity;
           
            $this->product[$pid] = $qt;
         }
      }
      $this->product[$prod] = $quantity;
      $this->save();
   }
?>
  1. <?php
  2.    function add($prod, $quantity)
  3.    {
  4.       $aray = $_SESSION['shopping_cart'];
  5.       foreach($aray as $pid => $qt)
  6.       {
  7.          if($prod == $pid)
  8.          {
  9.             $qt += $quantity;
  10.            
  11.             $this->product[$pid] = $qt;
  12.          }
  13.       }
  14.       $this->product[$prod] = $quantity;
  15.       $this->save();
  16.    }
  17. ?>
And now it saves only the last submitted result.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8398
  • Loc: USA

Post 3+ Months Ago

Ok... I'm making some progress here... I changed the add function to:
PHP Code: [ Select ]
<?php
function add($pid, $qt)
{
    if(isset($_SESSION['shopping_cart']))
    {
        $array = (array) $this->showSaved();
       
        foreach($array as $p_id => $quantity)
        {
            if($pid == $p_id)
            {
                $quantity += $qt;
            }
           
            $product[$p_id] = $quantity;
        }
    }
   
    $product[$pid] = $qt;
    $this->product = $product;
    $this->save();
}
?>
  1. <?php
  2. function add($pid, $qt)
  3. {
  4.     if(isset($_SESSION['shopping_cart']))
  5.     {
  6.         $array = (array) $this->showSaved();
  7.        
  8.         foreach($array as $p_id => $quantity)
  9.         {
  10.             if($pid == $p_id)
  11.             {
  12.                 $quantity += $qt;
  13.             }
  14.            
  15.             $product[$p_id] = $quantity;
  16.         }
  17.     }
  18.    
  19.     $product[$pid] = $qt;
  20.     $this->product = $product;
  21.     $this->save();
  22. }
  23. ?>

And the save function to:
PHP Code: [ Select ]
<?php
function save()
{
    $_SESSION['shopping_cart'] = $this->product;
}
?>
  1. <?php
  2. function save()
  3. {
  4.     $_SESSION['shopping_cart'] = $this->product;
  5. }
  6. ?>

Now it saves all of the orders in the order submitted... the only problem with this now, is that it doesn't add the quantity when the $pid is the same as $p_id...

[EDIT:] Nevermind, the following version of add fixed the problem.
PHP Code: [ Select ]
<?php
function add($pid, $qt)
{
   if(isset($_SESSION['shopping_cart']))
   {
      $array = (array) $this->showSaved();
     
      foreach($array as $p_id => $quantity)
      {
         if($pid == $p_id)
         {
            $quantity = $qt + $quantity;
            $ret = true;
         }
         
         $product[$p_id] = $quantity;
      }
   }
   
   if($ret == false)
   {
      $product[$pid] = $qt;
   }
   
   $this->product = $product;
   $this->save();
}
?>
  1. <?php
  2. function add($pid, $qt)
  3. {
  4.    if(isset($_SESSION['shopping_cart']))
  5.    {
  6.       $array = (array) $this->showSaved();
  7.      
  8.       foreach($array as $p_id => $quantity)
  9.       {
  10.          if($pid == $p_id)
  11.          {
  12.             $quantity = $qt + $quantity;
  13.             $ret = true;
  14.          }
  15.          
  16.          $product[$p_id] = $quantity;
  17.       }
  18.    }
  19.    
  20.    if($ret == false)
  21.    {
  22.       $product[$pid] = $qt;
  23.    }
  24.    
  25.    $this->product = $product;
  26.    $this->save();
  27. }
  28. ?>

Thanks for reading :D
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8398
  • Loc: USA

Post 3+ Months Ago

I've got another problem. I have a working version of a shopping cart... what I need right now is a way to pay. I need some payment system... will someone be kind enough as to provide me with a solution to this?
  • devilwood
  • Silver Member
  • Silver Member
  • User avatar
  • Posts: 436

Post 3+ Months Ago

I'm not sure how scalable the cart is but I just ran across an open source project called phpgcheckout.com which is a toolkit for integrating google checkout.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8398
  • Loc: USA

Post 3+ Months Ago

Alright... Now I can have google checkout (Thanks devilwood :D Much appreciated)... I will try to integrate that into my cart.

Now, how do I go about allowing for PayPal checkout?

[EDIT:] I just looked through that class and found out that it is a whole cart that uses google checkout... I don't need the cart... I have a cart, I just need some pointers as to how I could create the actual checkout. I would like to have PayPal... ... maybe Google checkout uses paypal and other types... I've never really looked into that thing... I will do that now

[EDIT 2:] lol, I got a little question about Google checkout. What I'm planning to do with my cart is allow users to submit their product to my store, set a price, and I would want to somehow let them get 90% of the profit and I will take 10% for them using my store service (Maybe 5%, I haven't decided yet), the point remains, that I want to split the profit each product generates between two accounts automatically... I wonder if Google checkout would allow something like this. Maybe I should have mentioned this at the very beginning but... I didn't :P

Thanks in advance for the help :D
  • devilwood
  • Silver Member
  • Silver Member
  • User avatar
  • Posts: 436

Post 3+ Months Ago

Sorry, I thought the google checkout toolkit was to help with the XML and DOM objects in order to pass the shopping cart info to google and receive google's XML response.

Now, google checkout and paypal are separate payment methods. I think Paypal is worth looking at because they are owned by ebay and that would make selling items on ebay a little easier. ALso, paypal offers payment gateways that will allow the user to stay on your site while they make the purchase. I think its the paypal payments pro, but I think it's limited to just the 50 US states which is kind of a downer. But you'd have to double check that.

You may want to check into a subscription fee based on the number of sales rather than a percentage of the take and actually have your customers that want to submit their products to your site subscribe to a recurring fee.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8398
  • Loc: USA

Post 3+ Months Ago

devilwood wrote:
Sorry, I thought the google checkout toolkit was to help with the XML and DOM objects in order to pass the shopping cart info to google and receive google's XML response.

Maybe it is... from a quick view of it, it looked like a cart.
  • devilwood
  • Silver Member
  • Silver Member
  • User avatar
  • Posts: 436

Post 3+ Months Ago

How's everything else comin'?

Did you get your array building like you wanted it? I messed around with it earlier this week but didn't get a solution.

I was trying to look into more info on session variables used with class objects. It seemed the session variable array was not getting added to. Did you get it straight?
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8398
  • Loc: USA

Post 3+ Months Ago

Check the last section of this post. I did fix the problem with that line of code. The array is working properly and now I found a whole directory of payment processors so I am almost finished with my shopping cart :D :D :D

Thanks for reading and trying to help me :D

Post Information

  • Total Posts in this topic: 10 posts
  • Users browsing this forum: No registered users and 102 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.