I have session to add product cart.
$getProductID = mysqli_real_escape_string($con, $_POST['productID']);
$getQuantity = mysqli_real_escape_string($con, $_POST['quantity']);
foreach($_POST as $key => $value)
{
$new_product[$key] = filter_var($value, FILTER_SANITIZE_STRING);
}
$qProduct = mysqli_query($con, "SELECT * FROM tb_product WHERE productid = '" . $getProductID . "'");
$dProduct = mysqli_fetch_array($qProduct);
$product_id = $dProduct['productid'];
$product_name = $dProduct['product_name'];
$product_price = $dProduct['product_price'];
$new_product["productid"] = $product_id;
$new_product["product_name"] = $product_name;
$new_product["product_price"] = $product_price;
$new_product["quantity"] = $getQuantity;
if(isset($_SESSION["products"]))
{
if(isset($_SESSION["products"][$new_product['productid']]))
{
unset($_SESSION["products"][$new_product['productid']]);
}
}
$_SESSION["products"][$new_product['productid']] = $new_product;
$total_items = count($_SESSION["products"]);
foreach($_SESSION["products"] as $product)
{
$product_quantity = $product["quantity"];
}
die(json_encode(array('items'=>$product_quantity)));
Then now I want to get session of product quantity
foreach($_SESSION["products"] as $product)
{
echo $product_quantity = $product["quantity"];
}
I can get the quantity as well, until I try to add another product cart it's not sum the quantity.
Example, on Product1 I add to cart 5pcs (I can see the quantity is 5)
and then I add Product2 to cart 3pc (It show me 53 that should be 8).
My question, how to sum the quantity even Product ID is different?
Change your loop as below, Sum inside loop and move echo outside of loop.
$product_quantity = 0;
foreach($_SESSION["products"] as $product)
{
$product_quantity += $product["quantity"];
}
echo $product_quantity;
I think because your coding have something wrong
//Incorrect
foreach($_SESSION["products"] as $product)
{
echo $product_quantity = $product["quantity"];
}
//first loop echo-ed 5
//second loop echo-ed 3
//so it show 53
//Correct
$product_quantity = 0;
foreach($_SESSION["products"] as $product)
{
$product_quantity += $product["quantity"];
}
echo $product_quantity;
Related
global $wpdb,$productname,$quantity,$total,$price,$product;
global $woocommerce,$product;
$items=WC()->cart->get_cart();
foreach($items as $item => $values) {
$cart_contents_count = WC()->cart->get_cart_contents_count();
if($cart_contents_count !=0){
$productname = $values['data']->get_title();
$productweight = $values['data']->get_weight();
$quantity = $values['quantity'];
$total = WC()->cart->subtotal;
$price = $values['data']->get_price();
}
I want to insert product name,productweight, quantity,total,price into database.
in the table every values inserted correctly but $productweight is always 0.
Can anyone help me please?
I am trying to get the results from my Foreach in another echo way later then the foreach but I am stuck, any help would be much appericated.
$items = $order->get_items();
// Output the loop
foreach ($order->get_items() as $item) {
// Getting some information
$product_qty = $item['qty'];
$product_variation_id = $item['variation_id'];
$product = new WC_Product($item['product_id']);
// SKU
$SKU = $product->get_sku();
print_r($SKU);
print_r($product_qty);
print_r(' ');
}
// this gives all 3 quantities and all 3 sku, added a space at the end for easier reading
// this only gives the 1st entry of both variables but i need all 3 of both variables
echo '<a href="https://www.domainname.someurl'.$product_quantity .$SKU . '" target=_blank>';
echo "<p>TEXT</p></a>";
I hope it is clear like this, thanks
Well, you're overwriting the value of $SKU and product_qty on each iteration, maybe try storing them in an array? Like create an array outsite the loop and use array_push(created_array, array($SKU, $product_qty)). Hope it helps.
$items = $order->get_items();
$push = array();
// Output the loop
foreach ($order->get_items() as $item) {
// Getting some information
$product_qty = $item['qty'];
$product_variation_id = $item['variation_id'];
$product = new WC_Product($item['product_id']);
// SKU
$SKU = $product->get_sku();
array_push($push, array('sku'=>$SKU, 'qty'=>$product_qty));
print_r($SKU);
print_r($product_qty);
print_r(' ');
}
// this gives all 3 quantities and all 3 sku, added a space at the end for easier reading
// this only gives the 1st entry of both variables but i need all 3 of both variables
$link = '<a href="https://www.domainname.someurl';
for ($i=0;$i<count($push);$i++){
$link .= $push[$i]['qty'];
$link .= $push[$i]['sku'];
}
$link .= '" target=_blank>';
echo $link;
Try the below code
$items = $order->get_items();
// Output the loop
$product_qty_string = '';
$sku_string = '';
foreach ($order->get_items() as $item) {
// Getting some information
$product_qty_string .= $item['qty']."-";
// SKU
$SKU = $product->get_sku();
$sku_string .=$SKU."-";
}
$product_quantity = rtrim($product_qty_string,'-');
$SKU = rtrim($sku_string,'-');
echo '<a href="https://www.domainname.someurl'.$product_quantity.''.$SKU.''" target=_blank>';
echo "<p>TEXT</p></a>";
Why don't you add your values in a array and then implode them.
$items = $order->get_items();
// Output the loop
$SKU = $product_qty = [];
foreach ($order->get_items() as $item) {
// Getting some information
$product_qty[] = $item['qty'];
$product_variation_id = $item['variation_id'];
$product = new WC_Product($item['product_id']);
// SKU
$SKU[] = $product->get_sku();
print_r($SKU);
print_r($product_qty);
print_r(' ');
}
$sku_string = implode('-', $SKU);
$product_qty_string = implode('-', $product_qty);
echo '<a href="https://www.domainname.someurl'.$product_qty_string .$sku_string . '" target=_blank>';
echo "<p>TEXT</p></a>";
You can implode your data with any separator you want, i use "-" for my example but its depending on your needs.
please check this demo. here one product with different color is not added into the cart but its updated the same product with different color which is not the right way.
here is code to add the product.
if(isset($_POST["product_code"]) && isset($_POST["product_color"]))
{
foreach($_POST as $key => $value){
$new_product[$key] = filter_var($value, FILTER_SANITIZE_STRING); //create a new product array
}
//we need to get product name and price from database.
$statement = $mysqli_conn->prepare("SELECT product_name, product_price FROM products_list WHERE product_code=? LIMIT 1");
$statement->bind_param('s', $new_product['product_code']);
$statement->execute();
$statement->bind_result($product_name, $product_price);
while($statement->fetch()){
$new_product["product_name"] = $product_name; //fetch product name from database
$new_product["product_price"] = $product_price; //fetch product price from database
$new_product["product_color"] = $_POST["product_color"];
if(isset($_SESSION["products"])){ //if session var already exist
$check = $_SESSION["products"][$new_product['product_code']][$new_product['product_color']];
if(isset($check)) //check item exist in products array
{
unset($check); //unset old item
}
}
$check = $new_product; //update products with new item array
}
$total_items = count($_SESSION["products"]); //count total items
die(json_encode(array('items'=>$total_items))); //output json
}
In above code how should I add the color of product also so that the one product with different color can be added?
list the items in cart code
if(isset($_POST["load_cart"]) && $_POST["load_cart"]==1)
{
if(isset($_SESSION["products"]) && count($_SESSION["products"])>0){ //if we have session variable
$cart_box = '<ul class="cart-products-loaded">';
$total = 0;
foreach($_SESSION["products"] as $product){ //loop though items and prepare html content
//set variables to use them in HTML content below
$product_name = $product["product_name"];
$product_price = $product["product_price"];
$product_code = $product["product_code"];
$product_qty = $product["product_qty"];
$product_color = $product["product_color"];
//$product_size = $product["product_size"];
//$cart_box .= "<li> $product_name (Qty : $product_qty | $product_color | $product_size ) — $currency ".sprintf("%01.2f", ($product_price * $product_qty)). " ×</li>";
$cart_box .= "<li> $product_name (Qty : $product_qty | $product_color ) — $currency ".sprintf("%01.2f", ($product_price * $product_qty)). " ×</li>";
$subtotal = ($product_price * $product_qty);
$total = ($total + $subtotal);
}
$cart_box .= "</ul>";
$cart_box .= '<div class="cart-products-total">Total : '.$currency.sprintf("%01.2f",$total).' <u>Check-out</u></div>';
die($cart_box); //exit and output content
}else{
die("Your Cart is empty"); //we have empty cart
}
}
Remove item from cart
if(isset($_GET["remove_code"]) && isset($_SESSION["products"]))
{
$product_code = filter_var($_GET["remove_code"], FILTER_SANITIZE_STRING); //get the product code to remove
if(isset($_SESSION["products"][$product_code]))
{
unset($_SESSION["products"][$product_code]);
}
$total_items = count($_SESSION["products"]);
die(json_encode(array('items'=>$total_items)));
}
In above code also how the color of product should be compared? e.g. Cool T-shirt | Blue and Cool T-shirt | Red and if Cool T-shirt | Red is removed then both should not be removed.
Can anyone please help me in this?
I am using the below code and want to increase the quantity in session array but its not working
if(!empty($_SESSION['quote']))
{
$prdata=$_SESSION['quote'];
}
$product_id = $data['id'];
$product_image = $data['image'];
$product_name = $data['pname'];
$product_quantity = $data['quantity'];
$product_price = $data['price'];
foreach ($_SESSION["quote"] as $cart_itm){
if($cart_itm['product_id'] == $product_id){ //the item exist in array
$newdat = array('product_id' => $product_id,'quantity'=>$product_quantity,'price'=>$product_price,'name'=>$product_name,'image'=>$product_image);
$found = true;
}else{
$newdat = array('product_id' => $product_id,'quantity'=>$product_quantity,'price'=>$product_price,'name'=>$product_name,'image'=>$product_image);
}
You don't need to put there product_id etc. again, change just amount. Your code will be shorter, clearer and you avoid to other possible typos, etc.
if($cart_itm['product_id'] == $product_id) {
$cart_itm['quantity'] += $product_quantity; // expect in $product_quantity is the amount you want to add to the current one
// it's a shorter variant of $cart_itm['quantity'] = $cart_itm['quantity'] + $product_quantity;
}
With a simple product I can do this:
$quote = Mage::getSingleton('checkout/session')->getQuote();
$item = $quote->getItemById($params['item']); // $params['item'] contains item id
$product = $item->getProduct();
$stockQty = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getQty();
However, if the item is a configured product, it tries to get the stock of its parent configurable product, which in most cases has an inventory of zero.
How can I get the stock quantity of a configured product, preferably without looping through its configurable parent's child products?
You can set a variable that determines the stock status depending on the stock status of the children of the configurable product:
$inStock = true;
$quote = Mage::getSingleton('checkout/session')->getQuote();
$cart = Mage::getModel('checkout/cart')->getQuote();
$item = $quote->getItemById($params['item']); // $params['item'] contains item id
$_product = $item->getProduct();
$configuredProduct = Mage::getModel('catalog/product_type_configurable')->setProduct($_product);
$children = $configuredProduct->getUsedProductCollection()->addAttributeToSelect("*")->addFilterByRequiredOptions();
foreach($children as $simpleProduct) {
foreach ($cart->getAllItems() as $item) {
if ($item->getProduct()->getId() != $simpleProduct->getId()) continue;
$stockQty = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($simpleProduct)->getQty();
if ($stockQty <= 0) {
$inStock = false;
break;
}
}
}