Error not all products are being printed in the shopping cart

  foreach, mysql, php, session

I have a working cart where I can add color and size, when I add a different color or size of the same product it is duplicated as if it were a different product due to the different options selected so far it works perfectly, as can be seen in the following print:

Array
(
    [2] => Array
        (
            [color-blue] => Array
                (
                    [M] => 1
                )

            [color-red] => Array
                (
                    [L] => 1
                )

        )

    [1] => Array
        (
            [color-blue] => Array
                (
                    [L] => 1
                )

        )

    [3] => Array
        (
            [] => Array
                (
                    [] => 1
                )

        )

)

In the last added article that can be seen in the array, it is a digital product, therefore that product has no size or color, on that side I think it is good that it is empty.

Now the problem is that it is only printing a product in the shopping cart and it is not printing the other products that are in the array, which were added to the shopping cart, as can be seen in the following image:

enter image description here

This is my cart code:

<?php
    session_start();
    echo "<pre>";
    print_r($_SESSION['cart']);
    echo "</pre>";

    $items = (isset($_SESSION['cart']) && is_array($_SESSION['cart'])) ? $_SESSION['cart'] : array();

    $itemCount = count($items);
    $total = 0;
    $num = 0;
    $iva = 12;
    $shipping = 50;
    $meCount = $itemCount;
    $meQty = 0;

    foreach($items as $id_product => $item) {
        foreach($item  as $color => $sizes) {
            foreach($sizes as $size => $quantity) {
                $meQty += $quantity;
            }
        }
    }

    $inputItems = array();
    $inputItems[] = implode(',',array_keys($items));
    $strMarcas  = str_repeat('?,', count(array($inputItems)) - 1) . '?';
    $strTipos = str_repeat('i', count(array($inputItems)));

    $stmt = $con->prepare("SELECT id_product, type_product, cover_product, title_product, subtitle_product, price_old, price FROM tbl_products WHERE id_product in ($strMarcas)");
    $stmt->bind_param($strTipos, ...$inputItems);
    $stmt->execute();
    $stmt->store_result();
    $outcome = array();
    $stmt->bind_result(
        $outcome['id_product'],
        $outcome['type_product'],
        $outcome['cover_product'],
        $outcome['title_product'],
        $outcome['subtitle_product'],
        $outcome['price_old'],
        $outcome['price']
    );

    while ($stmt->fetch()) {
        $id_article = $outcome['id_product'];
        foreach($items[$outcome['id_product']] as $color => $sizes){
            foreach($sizes as $size => $quantity){
                $price = $outcome['price'] * $quantity;
                $total += $price;

                //Note: I have removed the HTML to make the PHP code cleaner and easier to find the problem / error.
                echo $outcome['cover_product'];
                echo $outcome['title_product'];
                if ($color != null) { echo ' | ' . $color; }
                if ($size != null) { echo ' | ' . $size; }
                echo number_format($outcome['price'],2);
                echo $quantity;
                echo number_format($price,2);
            }
        }
    }
    $stmt->close();

Why is it not printing all the products that are in the array and it only prints one? What changes should I make or what step to ignore that is not working correctly for me?

Source: Ask PHP

LEAVE A COMMENT