Jump to content

adding a used item into a cart


divinedesigns1

Recommended Posts

hey, ok im trying to add the used price of an item within the cart that holds the original price. so i created a duplicated of the original price script so i can add the used price

if (isset($_POST['pid'])) {
    $pid = $_POST['pid'];
	$wasFound = false;
	$i = 0;
	// If the cart session variable is not set or cart array is empty
	if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
	    // RUN IF THE CART IS EMPTY OR NOT SET
		$_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "quantity" => 1));
        
        setcookie('id', $pid, time() + (86400 * 7));
        setcookie('quantity', '1', time() + (86400 * 7));
	} else {
		// RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
		foreach ($_SESSION["cart_array"] as $each_item) {
		      $i++;
		      while (list($key, $value) = each($each_item)) {
				  if ($key == "item_id" && $value == $pid) {
					  // That item is in cart already so let's adjust its quantity using array_splice()
					  array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1)));
                        setcookie('id', $pid, time() + (86400 * 7));
                        setcookie('quantity', $each_item['quantity'], time() + (86400 * 7));
					  $wasFound = true;
				  } // close if condition
		      } // close while loop
	       } // close foreach loop
		   if ($wasFound == false) {
			   array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1));
               setcookie('id', $pid, time() + (86400 * 7));
               setcookie('quantity', $each_item['quantity'], time() + (86400 * 7));
		   }
	}
	header("location: confirm.php");
    exit();
}

and this is the script for the used price

if(isset($_POST['uid'])){
    $use = $_POST['uid'];
    $use_product = $_POST['used'];
    $i = 0;
    $wasFound = false;
    if(!isset($_SESSION['cart_array']) || count($_SESSION['cart_array']) < 1){
        $_SESSION['cart_array'] = array(0 => array("item_id" => $use, "quantity" => 1, "price" => $use_product));
        header("location: confirm.php");
    }else{
        foreach($_SESSION['cart_array'] as $each_item){
            $i++;
            while(list($key, $value) = each($each_item)){
                if($key == "item_id" && $value == $use){
                    // that item is in the cart already adject quantity
                    array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $use, "quantity" => $each_item["quantity"] + 1, "price" => $use_product)));
                    $wasFound = true;
                    header("location: confirm.php");
                }
            }
        }
        // if no item is in the cart
        if($wasFound == false){
            array_push($_SESSION['cart_array'], array("item_id" => $use, "quantity" => 1));
            header("location: confirm.php");
        }
    }
}

I probably dont need both of these script right? and how can i excuted these prices so both can be added to the cart and not just add the same price

 

so pretty much original price is 20 and the used price is 10 but i keep on getting the total of 40 instead of 30

 

help please, no scripts please :)

Link to comment
Share on other sites

If they have different prices then you need to create different entries in the cart for each product/price, or otherwise split them up somehow so that you know how many of each at what price.

  • Like 1
Link to comment
Share on other sites

If they have different prices then you need to create different entries in the cart for each product/price, or otherwise split them up somehow so that you know how many of each at what price.

thanks

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...