Add to Cart PHP

Heys guys. How can I seperate the same product if the product size is not the same? For example I add a product, Nike KD Numbers Size 9 after that I will add a new; Nike KD Numbers Size 11. The cart content will be like this:

Nike KD Numbers
Price
Size: 11
Remove

Nike KD Numbers
Price
Size: 9
Remove

Jquery

//Add to Cart Module
function add_to_cart(product_id, action) {
	var quantity = $('#quantity'+product_id).val();
	var product_size = $('#product_size').val();
	var button = (action === 'Delete') ? $('#add_to_cart'+product_id).html('<i class="fa fa-shopping-cart"></i> Add to cart') : 
	$('#add_to_cart'+product_id).html('<i class="fa fa-check-square-o"></i> Added');
	button,setTimeout(function(){
		$.ajax({
			type: 'POST',
			url: '../pages/class.php',
			data: {product_id : product_id, quantity : quantity, product_size : product_size, action : action},
			cache: false,
			success: function(data) {
				console.log(data);
			}
		})
		$('#add_to_cart'+product_id).html('<i class="fa fa-shopping-cart"></i> 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);
}

Class.php

$product_id = $_POST['product_id'];
$product_size = $_POST['product_size'];
$quantity = $_POST['quantity'];
$action = $_POST['action'];

case 'Add':            
      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';
      } 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_image'],                                                                                                                                                         
            "product_brand"=>$row['product_brand'],
            "product_stocks"=>$row['product_stocks']
         );
     }
break;

Cart Content

You’ll need to expand the array so that instead of being keyed by product-id, it’s keyed by a combination of product-id and product-size, or some other unique combination. If not all products have a size, then you’d just use a dummy size to make things easier.

Doesn’t the SKU of the product reflect the size, for example one SKU would be product-id in size 11, and another would be that same product-id in size 12? Seems a nightmare for stock control, if not.

I would recommend storing size as an option instead.

          $_SESSION['item_cart'][$product_id] = array(
            "product_id"=>$product_id,
            "product_qty"=>$add_quantity, 
            "options"=> [ ['name'=> 'size', 'value'=> $product_size] ],
            "product_price"=>$row['product_price'],
            "product_name"=>$row['product_name'],
            "product_segment"=>$row['segment_id'],  
            "product_image"=>$row['product_image'],                                                                                                                                                         
            "product_brand"=>$row['product_brand'],
            "product_stocks"=>$row['product_stocks']
         );

Using options is much more flexible in terms of creating an interface that can be adapted to any product with any type of option like color, fit, etc.

Also is there any reason that product_ is used as a prefix for each key. I can’t see any reason why that would be necessary.

What should I do?

Will try this later. Thanks.

What will be the output of cart content if I add same product_id/product but different product size?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.