Click here to Skip to main content
15,997,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys. I'm developing a project which is online shop for shoes. How do I separate the product if the size is not the same. As you can see on the screenshot I have added Nike LeBron 15 'New Heights' Size 6. Now, assume that I will add another qty for that size, 2 quantities then it will automatically updates the quantity into 3. Again, assume that I will add Nike LeBron 'New Heights' Size 7. Now the problem is, how can I show the Nike LeBron 'New Heights' Size 7 in the cart details?

Screenshot:
cartdetails — imgbb.com[^]

What I have tried:

if(isset($_SESSION['item_cart'][$product_id]['product_id']) == $product_id && isset($quantity)) {
                                       $_SESSION['item_cart'][$product_id]['product_qty'] += $quantity;
                                       $qty = ($quantity == 1)  ?  'quantity' : 'quantities';
                                       notify([true,'#222','#fff',$row['product_name']. ' ' .'has been readded with '.$quantity.' '.$qty. '.']);
                                 } else {
                                       $count = (!isset($_SESSION['item_cart'])) ? 0 : count($_SESSION['item_cart']);
                                       $add_quantity = (isset($quantity)) ? $quantity : 1;
                                       $_SESSION['item_cart'][$product_id] = array(
                                              "product_id"=>$product_id,
                                              "product_qty"=>$add_quantity, 
                                              "product_size"=>$product_size,
                                              "product_price"=>$row['product_price'],
                                              "product_name"=>$row['product_name'],
                                              "product_segment"=>$row['segment_id'],  
                                              "product_image"=>$row['product_images'],                                                                                                                                                         
                                              "product_brand"=>$row['product_brand'],
                                              "product_stocks"=>$row['product_stocks'],
                                              "product_category"=>$row['category_name']
                                       );
                                       notify([true,'#222','#fff',$row['product_name']. ' ' .'has been added.']);
                                 }
Posted
Updated 27-Mar-18 4:04am
v2

The way I'd do it is to have each size have it's own separate part number:
PN      Desc
1000    Nike LeBron 'New Heights' Size 7
1001    Nike LeBron 'New Heights' Size 8
1002    Nike LeBron 'New Heights' Size 9
1003    Nike LeBron 15 Low Supernova Size 7
1004    Nike LeBron 15 Low Supernova Size 8
...
That way, you can check stock of specific sizes as well as ensure the right item is ordered and shipped.
 
Share this answer
 
What I tend to do is split the products into two tables: Product and SKU ("stock keeping unit"). The cart record links to the SKU record, rather than the product.

(You'd probably also want to normalize the products table, to extract common things like the make.)

So, in your example, you'd have:
Make
====
Make Id        Name
-------        ----
1              Nike

Product
=======
Product Number    Make Id    Description
--------------    -------    -----------
1000              1          LeBron 'New Heights'
1001              1          LeBron 15 Low Supernova
...

SKU
===
SKU Id      Product Number     Size
---         --------------     ----
1           1000               7
2           1000               8
3           1000               9
4           1001               7
5           1001               8
...

That way, it's easier to display 'New Heights' as a single product with a drop-down list of sizes, rather than displaying each size as a separate product.

NB: The cart shouldn't be storing the product details. All you need to store is the SKU Id, the quantity, and (optionally) the price.
 
Share this answer
 
Comments
John Th 29-Mar-18 3:36am    
So far this what I've tried. I have two tables which are tblproducts and tblproducts_extension. tblproduct_extension(product_id) is the foreign key of tblproducts(product_id). But it doesn't work. Any help?

Look at this screenshot.
https://ibb.co/d2RKS7
John Th 29-Mar-18 3:53am    
Add to Cart Button Screenshot
https://ibb.co/m6Wsx7

--JQuery/Ajax
function add_to_cart(product_id, status) {
	var quantity = $('#quantity'+product_id).val();
	var product_size = $('#product_size').val();
	var button = (status === 'Delete') ? $('#add_to_cart'+product_id).html(' Added');
	button,setTimeout(function(){
		var data = { action : 'Add To Cart', product_id : product_id, status : status, quantity : quantity, product_size : product_size };
		$.ajax({
			type: 'POST',
			url: '../pages/class.php',
			data: data,
			cache: false,
			dataType: 'json',
			success: function(data) {
				data.success == true ? successful(data.success,data.bgcolor,data.color,data.message) : successful(data.success,data.bgcolor,data.color,data.message);
			}
		});
		$('#add_to_cart'+product_id).html('^__i class="fa fa-shopping-cart"> Add to Cart');
		show_cart_count();
		show_cart_contents();
		show_cart_content_in_cart_page();
		show_cart_content_in_checkout_page();
		show_cart_total();
	},300);
}
John Th 29-Mar-18 4:01am    

--PHP Function
 if(isset($_POST['action']) && !empty($_POST['action'])) {
            $action = $_POST['action'];
            switch($action) {
                   case 'Add To Cart':
                          $product_id = $_POST['product_id'];
                          $product_size = $_POST['product_size'];
                          $quantity = $_POST['quantity'];
                          $status = $_POST['status'];
                          add_to_cart($product_id, $product_size, $quantity, $status);
                   break;
               }
     
 }

 function add_to_cart($product_id, $product_size, $quantity, $status) {
            GLOBAL $db_conn;
            $search_query = "SELECT p.*, pe.*, pct.* FROM tblproduct p JOIN (SELECT p.product_id, MIN(pe.product_extension_id) AS product_extension_id FROM tblproduct p LEFT JOIN tblproduct_extension pe ON pe.product_id = p.product_id WHERE p.product_id='$product_id' GROUP BY product_id) product_unique LEFT JOIN tblproduct_extension pe ON pe.product_extension_id = product_unique.product_extension_id LEFT JOIN tblproduct_carousel pct ON pct.product_id = p.product_id WHERE p.product_id = product_unique.product_id";
            $query = mysqli_query($db_conn, $search_query);
            $row = mysqli_fetch_array($query);

            if(isset($product_id)) {
                    switch ($status) {
case 'Add':
                                 if(isset($_SESSION['item_cart'][$product_id]['product_id']) == $product_id && $product_size && isset($quantity)) {
                                       $_SESSION['item_cart'][$product_id]['product_qty'] += $quantity;
                                       $qty = ($quantity == 1)  ?  'quantity' : 'quantities';
                                       notify([true,'#222','#fff',$row['product_name']. ' ' .'has been readded with '.$quantity.' '.$qty. '.']);
                                 } else {
                                       $count = (!isset($_SESSION['item_cart'])) ? 0 : count($_SESSION['item_cart']);
                                       $add_quantity = (isset($quantity)) ? $quantity : 1;
                                       $_SESSION['item_cart'][$product_id] = array(
                                              "product_id"=>$product_id,
                                              "product_qty"=>$add_quantity, 
                                              "product_size"=>$product_size,
                                              "product_price"=>$row['product_price'],
                                              "product_name"=>$row['product_name'],
                                              "product_segment"=>$row['segment_id'],  
                                              "product_image"=>$row['product_images'],                                                                                                                                                         
                                              "product_brand"=>$row['product_brand'],
                                              "product_stocks"=>$row['product_stocks'],
                                              "product_category"=>$row['category_name']
                                       );
                                       notify([true,'#222','#fff',$row['product_name']. ' ' .'has been added.']);
                                 }
                          break;
                    }
             }
      }
John Th 31-Mar-18 6:23am    
Help guys.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900