Jump to content

Adding shipping in php cart


maria

Recommended Posts

Hi There,

 

Following Jon's video tutorial I am trying to add shipping method to my cart(instead of using paypal I would rather have the order emailed to myself and the visitor). If the total is $100 + they get free delivery, however, if it is less, they get a different option. I used if - else statement but it threw me an error. Can anyone take a look at my code and see where I messed up?

 

Thanks,

Maria

 

 

 

<?

if($cart)

{

?>

(X)ItemSizeQuantityUnit PriceItem Subtotal

<?

$keys = array_keys($cart);

foreach($keys as $key)

{

$array = explode(".", $key);

$name = $array[0];

$size = $array[1];

$quantity = $cart["$key"];

 

$price = $xml->xpath("//item[@name=$name]/price[@size=$size]");

$unitPrice = (float) $price[0];

$itemSubtotal = $unitPrice * $quantity;

 

$total = $total + $itemSubtotal;

?>

 

remove

 

<? print($name); ?>

<? print($size); ?>

 

<? print($quantity); ?>

 

$<? printf("%0.2f", $unitPrice); ?>

 

$<? printf("%0.2f", $itemSubtotal); ?>

<?

}

?>

Total: $<? printf("%0.2f", $total); ?>

<?

 

 

?>

 

 

<?

}

else

{

?>

Your cart is empty!

<? } ?>

 

 

 

 

 

 

Link to comment
Share on other sites

Thanks for looking up quickly. Here's the error:

 

Parse error: syntax error, unexpected T_DOUBLE_ARROW in /Applications/xampp/xamppfiles/htdocs/XML/p7c/cart2.php on line 89

 

Also, I just copied that statement "if($cart) {...} else {print "Your Cart is empty!}" somewhere that I don't remember now. Is that where the problem could be? I am repasting the code where I added the if and else statement that didn't work. If the total is equal to or more than $100, it should take them to the delivery page, but if not, then to pick-up page. Or is there any better alternative?

 

I really appreciate your help.

 

Maria

 

-----

 

 

<?

$xml = new SimpleXMLElement(file_get_contents("store.xml"));

 

session_start();

 

$cart = $_SESSION["cart"];

?>

<? require_once("head.php"); ?>

 

 

 

 

<?

if($cart)

{

?>

(X)ItemSizeQuantityUnit PriceItem Subtotal

<?

$keys = array_keys($cart);

foreach($keys as $key)

{

$array = explode(".", $key);

$name = $array[0];

$size = $array[1];

$quantity = $cart["$key"];

 

$price = $xml->xpath("//item[@name=$name]/price[@size=$size]");

$unitPrice = (float) $price[0];

$itemSubtotal = $unitPrice * $quantity;

 

$total = $total + $itemSubtotal;

?>

 

remove

 

<? print($name); ?>

<? print($size); ?>

 

<? print($quantity); ?>

 

$<? printf("%0.2f", $unitPrice); ?>

 

$<? printf("%0.2f", $itemSubtotal); ?>

<?

}

?>

Total: $<? printf("%0.2f", $total); ?>

 

<?

 

 

?>

 

 

<?

}

else

{

?>

Your cart is empty!

<? } ?>

 

<?

if ($total=>100)

{

header ('Location: Delivery.php');

}

else

{

header ('Location: pickUp.php');

}

 

?>

 

 

 

 

 

 

 

 

 

 

<? require_once("end.php"); ?>

Link to comment
Share on other sites

the issue seems like it is with this "=>" in your if statement. It needs to be this instead: ">=".

 

However, I'm guessing that fixing this error will then lead to a "headers already sent" error. To be honest, I am not totally sure what you are trying to do here... according to your above code, it would display part of the cart page, and then immediately redirect them to either the delivery or pickup page based on their order total. The visitor wouldn't even see the card page. Wouldn't you want to redirect them after they click the "checkout" button?

Link to comment
Share on other sites

Hi Benjamin,

 

When you said: Wouldn't you want to redirect them after they click the "checkout" button?

 

Yes, that's what I really wanted to do, I just could not figure out how to say it right. I always want the visitor to see the cart page all the time. Would you suggest that I should create another checkout page with all same info where all these things happen? Then I'll try the ">=". and see what happens.

 

Many thanks,

Maria

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...