show display nothing from cart menu sidebar when first i click on add to cart.
show display from cart menu sidebar when second i click on add to cart.
URL: https://www.bellastudio.pk/new/products.php?p_slug=olive-green
when i ty to start the add to cart i get this error:
PHP Warning: array_column() expects parameter 1 to be array, null given in
PHP Warning: array_search() expects parameter 2 to be array, null given in
i don’t know how to make’it work, some help is appreciated. I’m new with this.
PHP
try { if(isset($_POST['product_name'])) { $name = $_POST['product_name']; } if(isset($_POST['product_price'])) { $price = $_POST['product_price']; } if(isset($_POST['product_itemno'])) { $itemno = $_POST['product_itemno']; } if(isset($_POST['product_quantity'])) { $qty = $_POST['product_quantity']; } if(isset($_POST['product_color'])) { $color = $_POST['product_color']; } if(isset($_POST['product_size'])) { $size = $_POST['product_size']; } if(isset($_POST['sleeves'])) { $sleeves = $_POST['sleeves']; } if(isset($_POST['neck'])) { $neck = $_POST['neck']; } $stockitemno = $itemno.'-'.$size; $host = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); $host->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $host->prepare( "SELECT * FROM productstock INNER JOIN product ON product.p_id = productstock.pid INNER JOIN productsize ON productsize.sid = productstock.size_id WHERE product.p_itemno = :itemno AND productsize.sname = :size" ); $stmt->bindValue( ':itemno', $itemno ); $stmt->bindValue( ':size', $size ); $stmt->execute(); while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ) { // Current stock quantity $stockqty = $row[ 'stockqty' ]; // No stock, bail early if ( !$stockqty ) { echo '<p>No available stock for that item.</p>'; } // User didn't provide a quantity value else if ( !$qty ) { echo '<p>Please enter a quantity</p>'; } // Lets add to the cart else { // Find product in cart $cartindex = array_search( $stockitemno, array_column( $_SESSION[ 'cart' ], 'stockitemno' ) ); // Product info $product = null; // If already exists in cart if ( $cartindex !== false ) { $product = $_SESSION[ 'cart' ][ $cartindex ]; } // Does not exit in cart, create new product info else { $product = [ 'name' => $name, 'price' => $price, 'item_no' => $itemno, 'qty' => 0, // Default to none added 'color' => $color, 'size' => $size, 'stockitemno' => $stockitemno, 'sleeves' => $sleeves, 'neck' => $neck, 'stockqty' => $stockqty ]; } // Track how many items were able to be added to the cart $totaladded = 0; // If users full amount is available if ( ( $product[ 'qty' ] + $qty ) <= $stockqty ) { $totaladded = $qty; } // Else add all available stock else if ( $stockqty - $product[ 'qty' ] ) { $totaladded = ( $stockqty - $product[ 'qty' ] ); } // If we were able to add new items to cart if ( $totaladded ) { // Update product new qty $product[ 'qty' ] += $totaladded; // Update cart item if ( $cartindex !== false ) { $_SESSION[ 'cart' ][ $cartindex ] = $product; } // Add new item to cart else { $_SESSION[ 'cart' ][] = $product; } // Example: 5 item (size) were successfully added to your shopping cart. printf( '<p><strong>%d</strong> %s (%s)<br>was successfully added to your shopping cart.</p>', $totaladded, $name, $size ); } else { echo '<p>Not enough stock</p>'; } } } } catch ( PDOException $e ) { echo 'Error: ' . $e->getMessage(); } $host = null; $cart_count = count( $_SESSION[ 'cart' ] );
AJAX
$(document).ready(function() { load_cart_data(); function load_cart_data() { $.ajax({ url:"asset/includes/fetch_cart1.php", method:"POST", success:function(data) { $('.cart_details').html(data); $('.badge1').text(data.total_item); } }); } $(document).on('click','#add_to_cart',function (e) { var cart_count = $('.navbar-tool-badge').text(); cart_count = parseInt(cart_count); if($('input[type=radio][name=size]:checked').length == 0) { $('.msg').html('Please choose size.'); return false; } else { var product_name = $('#hidden-name').val(); var product_price = $('#hidden-price').val(); var product_itemno = $('#itemno').val(); var product_quantity = $('.quantity').val(); var product_color = $('#color').val(); var product_size = $("input[name='size']:checked").val(); var sleeves = $('#sleeves').val(); var neck = $('#neck').val(); e.preventDefault(); $.ajax ({ type: "POST", url: "asset/includes/insertcart.php", data:{product_name:product_name, product_price:product_price, product_itemno:product_itemno , product_quantity:product_quantity, product_color:product_color, product_size:product_size, sleeves:sleeves, neck:neck}, cache: false, success: function(response) { load_cart_data(); $("#getCode").html(response); $("#myModal").modal('show'); // remove cart count $('.navbar-tool-badge').html(cart_count + 1); } }); } }); $(document).on('click', '.delete', function(){ var item_no = $(this).attr("id"); var cart_count = $('.navbar-tool-badge').text(); cart_count = parseInt(cart_count); $.ajax({ url:"asset/includes/delete_item.php", method:"POST", data:{item_no:item_no}, success:function(data) { if(data){ load_cart_data(); $('#cart-popover').popover('hide'); } // remove cart count $('.navbar-tool-badge').html(cart_count - 1); } }) }); });
Advertisement
Answer
at the first attempt, your $_SESSION[ ‘cart’ ] might be empty and hence you are getting an error. try to validate for empty cart and then do a search like this
if(!empty($_SESSION[ 'cart' ])){ $cartindex = array_search( $stockitemno, array_column( $_SESSION[ 'cart' ], 'stockitemno' ) ); }