Add to Cart, with Multiple Variants - how?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Add to Cart, with Multiple Variants - how?

Post by simonmlewis »

Code: Select all

<?php
session_start();
$depth = "0";
if(isset($_GET['depth']))
{
    $depth = $_GET['depth'];
    $_SESSION['depth']=$depth;
} 
else
{
    $depth=$_SESSION['depth'];
}
This causes same issue.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Add to Cart, with Multiple Variants - how?

Post by simonmlewis »

I can see why that isn't going to work tho. As I am assigning 0 to depth, but then I am saying, if there is nothing in $_get['depth'], then $depth = session.

But there is nothing assigned to the session.
Can I assign $depth to a session before the start of the statement??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Add to Cart, with Multiple Variants - how?

Post by Celauran »

Code: Select all

$depth = "0";
if(isset($_GET['depth']))
{
    $depth = $_GET['depth'];
    $_SESSION['depth']=$depth;
} 
else if (isset($_SESSION['depth']))
{
    $depth=$_SESSION['depth'];
}
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Add to Cart, with Multiple Variants - how?

Post by simonmlewis »

Great stuff, thanks.
Just got to find out from the client if their current system uses Sage's cart tools, or if it needs sessions to store IDs, and then post them to a Sage page.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Add to Cart, with Multiple Variants - how?

Post by simonmlewis »

I'm now trying to make it so that if there is no depth assigned to a set of products, and is only using Length and Thickness of the shelves, and the 'Depth' dropdown therefore doesn't show, I still need the ajax script to run.

Code: Select all

<?php
session_start();

$depth = "0";
if(isset($_GET['depth']))
{
    $depth = $_GET['depth'];
    $_SESSION['depth']=$depth;
}
else if (isset($_SESSION['depth']))
{
    $depth=$_SESSION['depth'];
}



$length = "0";
if(isset($_GET['length']))
{
    $length = $_GET['length'];
    $_SESSION['length']=$length;
}
else if (isset($_SESSION['length']))
{
    $length=$_SESSION['length'];
}


$thickness = "0";
if(isset($_GET['thickness']))
{
    $thickness = $_GET['thickness'];
    $_SESSION['thickness']=$thickness;
}
else if (isset($_SESSION['thickness']))
{
    $thickness=$_SESSION['thickness'];
}


include "dbconn.php";
if ($depth > "0" && $length > "0" && $thickness > "0")
{
  $query = "SELECT price, productid, sku FROM products WHERE depth = '$depth' AND length = '$length' AND thickness = '$thickness'";
  $result = $pdo->prepare($query);
  $result->execute(array(':depth' => "%{$depth}%",':length' => "%{$length}%",':thickness' => "%{$thickness}%"));
  $num_rows = $result->rowCount();
  if ($num_rows == 0) 
    { 
    echo "<br/>Sorry we don't have your size available.";
    }
  else
    {
    echo "<div class='presearchbox' >";
    while ($row = $result->fetch(PDO::FETCH_OBJ)) 
      { 
      echo "<div class='premier_price'>&pound;$row->price</div>
      <a href='/premiercart&productid=$row->productid' class='premier_cart'>Add to Cart</a>";
      }
    echo "</div>";
    }
}
mysql_close($sqlconn);
?>
The problem is, if Depth isn't posted through, it assigns zero to depth. But the database won't have a zero assigned to depth, it will be NULL.

So how do I make it query the database for that set of products, if:
a) nothing is posted for depth
b) nothing is in the DB row for depth for this group and;
c) ensure when there ARE depth (and the others) available to be posted thru, it works only if depth is posted.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Add to Cart, with Multiple Variants - how?

Post by simonmlewis »

I've written a way for it to check if the product SKU group in question has a Depth, Length or Thickness element in the DB.

But further down it's still a problem. It's asking for it in the variable. The DB field will be a NULL value if nothing entered, but because of that, the $depth being NULL, isn't causing an error of not showing anything because the array is asking for it:

Code: Select all

$query = "SELECT price, productid, sku FROM products WHERE depth =:depth AND length =:length AND thickness =:thickness AND sku =:sku";
  $result = $pdo->prepare($query);
  $result->execute(array(':depth' => $depth,':length' => $length,':thickness' => $thickness,':sku' => $sku));
  $num_rows = $result->rowCount();
  if ($num_rows == 0) 
    { 
    echo "asdfsadf, $depth, $length, $thickness";
    }
  else
    {
    echo "<div class='presearchbox' >";
    while ($row = $result->fetch(PDO::FETCH_OBJ)) 
      { 
      echo "<div class='premier_price'>&pound;$row->price</div>
      <a href='/premiercart&productid=$row->productid' class='premier_cart'>Add to Cart</a>";
      }
    echo "</div>";
    }
How do I get around that, without create about 9-12 different $result arrays at the end - one for each scenario.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Add to Cart, with Multiple Variants - how?

Post by simonmlewis »

Right I think I have this pinned down now.
The next job is to create an actual Cart.
We've never needed to do this as we always use third parties. But their current site has it on the cart, via SSL.

The only way I can see how it would work, is via a Database "cart" table, that does it via a Session ID number. So if you do login, it then overrides the Session ID with the User ID.

That way you can add all the items you want, all assigning to that one Sess/User ID.
But how do you then pass that information over to PayPal or Sage, and see the products you have ordered, on said third parties - I'm really stuck there.
I assume they are waiting for content of variables, but I'm lost here.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Add to Cart, with Multiple Variants - how?

Post by Christopher »

simonmlewis wrote:Right I think I have this pinned down now.
The next job is to create an actual Cart.
We've never needed to do this as we always use third parties. But their current site has it on the cart, via SSL.

The only way I can see how it would work, is via a Database "cart" table, that does it via a Session ID number. So if you do login, it then overrides the Session ID with the User ID.

That way you can add all the items you want, all assigning to that one Sess/User ID.
I have built carts that session and database. Using the session is simpler because there is little housekeeping involved. Carts using a database table are useful if you want the user to be able to save the contents of the cart between sessions.
simonmlewis wrote:But how do you then pass that information over to PayPal or Sage, and see the products you have ordered, on said third parties - I'm really stuck there.
I assume they are waiting for content of variables, but I'm lost here.
The payment services provide both code and instructions for checkout.
(#10850)
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Add to Cart, with Multiple Variants - how?

Post by simonmlewis »

I agree - I will be doing it with a Database, because it also means if they are logged in and swap between PC and Phone, they will still see their cart.

"The payment services provide both code and instructions for checkout".
Really? I have yet to find out how to pass the product titles and pricing, when you have various products in your cart, to PayPal or Sage. This is where I am stuck.

I am looking at OpenCart as well, as that might just be easier to implement a Cart and gateway.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Add to Cart, with Multiple Variants - how?

Post by simonmlewis »

Looking at the various options, I don't think it's possibly to do what I want.
I see many sites that are built professionally, and they look like they were designed ad-hoc, and a cart written, and then payment processed inhouse thru PayPal or Sage etc.

I cannot see how this is done though.

I've been looking at X-Cart. This seems to provide a full-on set of code that you install, customize the CSS and imagery, upload products etc, and it somehow just works with these payment gateways.

Is this a good solution, or is there some off-the-shelf method of Cart that can be installed on an existing site, that works WITH it?

Or... how do you post data to PP or SP that collects the data. It's stumped me big time.
:banghead:
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Add to Cart, with Multiple Variants - how?

Post by Celauran »

What are you stuck on specifically? You're storing the user's cart server side, so you can send PayPal some custom ID with which you can retrieve it after.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Add to Cart, with Multiple Variants - how?

Post by simonmlewis »

I'm storing the cart in DB rows. So it's stores their session ID. If they then logging, it replaces the session ID with their Userid, so they could look at it on their phone and it will show the same info.

So let's say they have 5 items in their Cart, each with it's own SKU.

I know how I can show one button if they select PayPal and one button if they select Sagepay - it's what I use as a form to post all the information to PayPal with, so that when they are on PayPal, it would show the pricing and product name.

I'm sure I have to post custom information thru to PayPal, one per product/price, but I've no idea of the code.

Same goes for Sage.

If I can get thru this problem, I'm kinda ok.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Add to Cart, with Multiple Variants - how?

Post by Celauran »

Details about the individual items?
Like this?

Code: Select all

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="business" value="youremail@mail.com">
<input type="hidden" name="currency_code" value="US">

<input type="hidden" name="item_name_1" value="beach ball">
<input type="hidden" name="amount_1" value="15">

<input type="hidden" name="item_name_2" value="towel">
<input type="hidden" name="amount_2" value="20">

<input type="image" src="http://www.paypal.com/en_US/i/btn/x-click-but01.gif" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
</form>
Or do you mean something else entirely?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Add to Cart, with Multiple Variants - how?

Post by simonmlewis »

No that might be it for PayPAl. Plus would need to add postage, which is free for UK and set prices for other locations.
Not sure though, how, when they return to the main site, the site can trigger the items as purchased on the web site.

It's also open the abuse I suppose, with the Value fields being editable. Tho I guess with a listener, that could query that.

what about Sagepay?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Add to Cart, with Multiple Variants - how?

Post by Celauran »

I've never used Sagepay. I'm sure they have documentation.
Not sure though, how, when they return to the main site, the site can trigger the items as purchased on the web site.
Use their cart ID or some custom order number that you've set beforehand. Send that along with the PayPal request, either as order number or as a custom variable (literally input name=custom) and then retrieve the items ordered when you get the PayPal response.
Post Reply