SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2008
    Location
    Dublin, Ireland.
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to delete item from checkout

    Hi all,

    I've been building a shopping cart site and I'd like to add a delete button to each item in the checkout page to give the customers an easy way to dump an item they decide they don't want.

    Can anyone please give me an idea how to go about this.

    I'm thinking something to do with the $_SESSION['cart']['id'] but I can't figure it out.

    Thank you very much.

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    How are you storing the data within your cart? You should be able to just unset the item within the session array though...
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2008
    Location
    Dublin, Ireland.
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that fast reply Silverb.

    I know what you mean and its really bugging me that I haven't managed it yet and your reply spurs me on to go back and try again.

    So I'll give it another go.

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Here's a quick example to hopefully help you a little.

    PHP Code:
    <?php

    //--> Suggested cart structure, where 23,12 and 19 are the unique product identifers.
    $aCart = array(
        
    'cart' => array(
            
    'items'    =>    array(
                
    23    =>    array(
                    
    'quantity'        =>    3,
                    
    'price'            =>    1000,
                    
    'description'    =>    'Ice-Lollies'
                
    ),
                
    12    =>    array(
                    
    'quantity'        =>    1,
                    
    'price'            =>    56,
                    
    'description'    =>    'Ice-Cream'
                
    ),
                
    19    =>    array(
                    
    'quantity'        =>    31,
                    
    'price'            =>    950,
                    
    'description'    =>    'Ice-Cubes'
                
    )
            )
        )
    );

    //--> Display total cart value.
    foreach ($aCart['cart']['items'] as $aCartItem)
    {
        
    $iCartValue += $aCartItem['quantity'] * $aCartItem['price'];
    }
    echo 
    sprintf('£%01.2f'$iCartValue 100);

    //--> Remove an item altogether
    unset($aCart['cart']['items'][12]); #No more Ice-Cream :o(

    //--> Display number of unique items in cart.
    echo 'Your cart contains ' count($aCart['cart']['items']) . ' unique items.';

    //--> Deduct one Ice-Cube.
    $aCart['cart']['items'][19]['quantity'] = ($aCart['cart']['items'][19]['quantity'] > 0) ? $aCart['cart']['items'][19]['quantity']-- : ;

    //--> Increase Ice-Lolly quantity by one.
    $aCart['cart']['items'][23]['quantity']++;

    //--> Empty cart
    $aCart['cart']['items'] = array();

    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2008
    Location
    Dublin, Ireland.
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's so cool SilverB.

    I'm chuckling quietly away at the ingenuity.

    I was just patting myself on the back because I'd got a solution with your suggestion:-

    if(isset($_GET['id']))
    {
    $id=$_GET['id'];
    }
    unset($_SESSION['cart'][$id]);

    Then your script comes along and the sky is the limit.

    I'll have to start thinking bolder and bigger for the new year!

    Thank you very much.

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You're very welcome, however it looks like you'll do just fine!

    Happy New Year to you too.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •