DevelopPHP.com 2010 Discussion Archive(not active)

 

Forum Index >> PHP and MySQL Forum

E-commerce series. How to hide the empty cart button
   THREAD CLOSED
E-commerce series. How to hide the empty cart button
by yuukan - Sun Feb 13, 2011 03:07:21 AM
Hi
when the shopping cart is empty I would like to hide the "empty cart" button. Otherwise, it will appear.
but when it's empty,  I get an error:
Notice: Undefined index: cart_array in cart.php on line 288

I have:
<?php
// Shows or hide the empty cart button
if (count($_SESSION["cart_array"])>=1){ // line 288
echo '<a href="cart.php?cmd=emptycart"><img src="images/empty_cart.gif" width="128" height="29" alt="" /></a>';
  }
?> 

cart_array was defined before in cart.php so I don't know how to fix it. Maybe I should rewrite the previous script
anyway, I appreciate any help

thanks 

RE: E-commerce series. How to hide the empty cart button
by yuukan - Wed Feb 16, 2011 04:34:22 AM
It was easy, here the code:

<?php 
//Shows or hide the empty cart link or button
if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1){
echo exit();//Here you can show whatever you want
}else{
echo '<div style="float:left;"><a href="cart.php?cmd=emptycart">Empty cart</a></div>'; 
}
?>

RE: E-commerce series. How to hide the empty cart button
by AudioPro - Wed Feb 16, 2011 10:06:15 AM
This was one of the first adjustments I made to the cart and it's nearly identical to your method with just a few differences.  My problem was that the cart and most of my pages are inside an index container so using the get method screwed up the query string and broke all the page; styling, includes, etc.  So here is the method I came up with....

First, I had to change empty cart from a _GET to a _POST.  This job was easy enough.  You're not really passing any data that doesn't already exist inside the session cookie so I don't see where any sort of data cleansing or extra security measures would be needed. But if they are let me know.

<?php // if user chooses to empty their shopping cart
if (isset($_POST['empty_cart'])) {
    unset($_SESSION["cart_array"]);
}
?>

Now, we make the actual button itself.  This is where it's scary close to yours.  Great minds think alike I guess!  Anyway, because it's a post operation you need to change it to an input form. So you wind up with this....

<?php  // The actual empty cart button
if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"])<1) {
    echo "Your cart is currently empty.";
} else {
    echo "<form action='index.php?cart' method='post'>  // the form action may need to be changed to cart.php in your case
<input name='emptyCart' type='submit' value='Empty Cart' />
<input name='empty_cart' type='hidden' value='' />
</form>";
    }
?>

Last thing you want to do is to make sure that the button appears instead of the h1 block notification.  This again is pretty simple.  Just go to the 6th line of code where it says "$cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>";"  And change it to an empty set of quotes as shown below.

<?php // section render the cart for the user to view
.
.
.
$product_id_array = '';
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"])<1) {
    $cartOutput="";          //  <--- make this line an empty pair of quotes.
} else {
    // Start PayPal Checkout Button
    $pp_checkout_btn .= '<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=" ">'; // value = you@youremail.com
    // Start the For Each loop
.
.
.
?>

And that's all there is to it!

-Carl



RE: E-commerce series. How to hide the empty cart button
by yuukan - Wed Feb 16, 2011 04:32:15 PM

Thanks for sharing Carl!

I will experiment with your code.

Actually, you give me the idea about the empty set of quotes. 
I found this code to show "items in cart", I just added the empty set of quotes:

<?php
//items in cart
session_start();
if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1){
  $items_in_cart = ""; //  doesn't show anything. it's useful because I don't want to show "items in cart: zero"
} else {
$a = 0;
foreach ($_SESSION["cart_array"] as $each_item) {
$a = $a + $each_item['quantity'];
}
 $items_in_cart='You have' . $a . 'items in cart';

}
?>


One more question, this is not the thread, but  how do you deal with the stock?
I mean do you have a script to automate it or do you  change the inventory manually?
I would like to know  the logic you are using to control the inventory, just to learn xD

thanks



RE: E-commerce series. How to hide the empty cart button
by gotmburl - Wed Feb 16, 2011 07:00:26 PM
Hey Guys check this out if you haven't already seen it.

http://www.developphp.com/forum/view_topic.php?cat=3&otid=39867&title=E-Commerce%20-%20How%20To%20Control%20Max%20Quantities%20For%20Products

Don't have mine working yet but I am sure it's me and not DougDoug.  I have one question if anyone cares to offer some assistance. I am using DougDoug's script but on his products page he is not using a dynamic list so I am stuck.  All of his code follows Adams nicely but not sure why there is no dynamic list?  My problem is now my add to cart button has disappeared.  Not sure how to handle the " echo cart".  If it's just me sorry for wasting your time but if you have the same problem please share how you are handling it.  I will also post if I find the fix.  

RE: E-commerce series. How to hide the empty cart button
by AudioPro - Wed Feb 16, 2011 08:08:43 PM
[quote]
One more question, this is not the thread, but  how do you deal with the stock?
I mean do you have a script to automate it or do you  change the inventory manually?
I would like to know  the logic you are using to control the inventory, just to learn xD

[/quote]

Actually, I haven't really started working on that part just yet. Real life kinda getting in the way.   Apparently you can do it in some way through the paypal ipn script.  What I'd like to have it do is when an order confirmation gets sent back a piece of the code connects to the database and decrements the value by 1 or however many items are ordered.  Then of course it'd be automatically reflected on my product page from the other script I had problems with yesterday.

As for general inventory control, the logic would go something like this....

you've got a starting quantity called "stock" in you database.
this value is shown on the product page as quantity or stock on hand, along with an add to cart button.
the order is processed and confirmation sent back thru the IPN script
the stock field is decremented by N units as per the purchase
now the new quantity is shown on the product page and the loop goes on.

This should all be pretty basic in terms of scripting, unless you're referring to something more in line with a full on CMS based E-commerce package like zencart or oscommerce where you've got complete admin control over any number of parameters.

   THREAD CLOSED  
©2009 DevelopPHP.com