In e-commerce websites, it is all about usability and conversion. A micro feature can stand between you and profitability. We need to facilitate buyer as much as possible by adding needed features to the and its flow process. For example, by providing drag and drop can be a killer feature in a cart and increases the sales. Previously, in a PHP shopping cart example, I have added an input to enter the product quantity with increment and decrement option. Like this you need to look for opportunities to make the buyer comfortable.

bulk-money  - bulk money - Add Multiple (Bulk) Product into Shopping Cart using PHP

View Demo

Adding products to the cart will help the user to upload. I have added a checkbox for each item in the gallery. The user will select the by using this checkbox. Once the user finalizes the list of products he wants to buy, he can move them all together to the cart stack by clicking the add-to-cart button. 

Product Gallery with Bulk Add-to-Cart Option

This HTML code is used to show the product gallery after reading the products from the database. Each product item is displayed as a card with a checkbox option. This checkbox is used to select the product if the user wants to move it to the cart. After selecting favourite products by using these checkboxes, the Add to Cart submit button is used to send the bulk of selected products to the .

<div id="product-grid">
    <div class="txt-heading">
        <div class="txt-heading-label">Products</div>
    $query = "SELECT * FROM tbl_product";
    $product_array = $shoppingCart->getAllProduct($query);
    if (! empty($product_array)) {
        <form method="post" action="index.php?action=add">
        foreach ($product_array as $key => $value) {
        <div class="product-item"
                id="product-item-<?php echo $product_array[$key]["id"]; ?>">

                <div class="product-image">
                        src="<?php echo $product_array[$key]["image"]; ?>"
                        onclick="selectImage(<?php echo $product_array[$key]["id"] ?>);" />
                    <div class="product-title">
                        <input type="checkbox" class="checkstatus"
                            -id="<?php echo $product_array[$key]["id"]; ?>"
                            -code="<?php echo $product_array[$key]["code"]; ?>"><?php echo $product_array[$key]["name"]; ?></br>
			<?php echo "$".$product_array[$key]["price"]; ?></br>

                <input type="hidden" name="selected_product[]"
                    id="product-<?php echo $product_array[$key]["id"]?>"
                    value="" />

            <input type="submit" name="add_to_cart" value="Add to Cart"
                class="btn-submit" />

This jQuery script is used to highlight the product item by clicking the checkbox. Also, it sets the selected product ident in a hidden field that is accessed by PHP after submitting the selected products.

	$("input[type=checkbox]").on("click", function() {
		var product_id = $(this).data("id");
		var product_code = $(this).data("code");

            	$("#product-item-"+product_id).css("border","#d96557 3px solid");
        	else if($(this).is(":not(:checked)")){
            	$("#product-"+product_id).val("	");

PHP Code to process Bulk Add to Cart

After submitting the bulk number of products via the HTML form, PHP receives the selected product array and process bulk add-to-cart action. I used the persistent cart with the help of the database to preserve the selected cart item even after user session expired. I have iterated the selected product array and process add-to-cart action in each iteration. While adding products to the cart, I check if the product already exists in the cart. If so, I update it by increasing its quantity instead of adding a new entry.

$shoppingCart = new ShoppingCart();
if (! empty($_GET["action"])) {
    switch ($_GET["action"]) {
        case "add":
            if (! empty($_POST["selected_product"])) {
				foreach($_POST["selected_product"] as $product_code) {
					$productResult = $shoppingCart->getProductByCode($product_code);
					$cartResult = $shoppingCart->getCartItemByProduct($productResult[0]["id"], $member_id);
					if (! empty($cartResult)) {
						// Update cart item quantity in database
						$newQuantity = $cartResult[0]["quantity"] + 1;
						$shoppingCart->updateCartQuantity($newQuantity, $cartResult[0]["id"]);
					} else {
						// Add to cart table
						$shoppingCart->addToCart($productResult[0]["id"], 1, $member_id);
        case "remove":
            // Delete single entry from the cart
        case "empty":
            // Empty cart

PHP Bulk Add-to-Cart Output

This screenshot shows the shopping cart product gallery with the selected products. The selected products are in checked status and highlighted in the screenshot.

php-bulk-add-to-cart-output  - php bulk add to cart output - Add Multiple (Bulk) Product into Shopping Cart using PHP

View DemoDownload

This PHP code was published on March 26, 2018.

↑ Back to Top

Source link


Please enter your comment!
Please enter your name here