1

For a long time without developing, soon after the resumption I find myself encountered with a big problem that I find in my project in which I am now developing in PHP, which consists of the use of $_COOKIE to save information from a wish list and shopping cart on a page, through requests $_GET or $_POST. I found the request $_GET the simplest way to save and redirect information to specific places within the schedule, such as for a session variable or even to a database.

I am through the following challenge, store information of items (books) in the shopping cart when added by the customer and still to complete, save the desired products by the same to later buy or even make some order with it. It's a complex problem to solve or I'm making it difficult for all programming logic to find a solution to solve this case myself.

Being a little more specific regarding the specifications of the problem, I have to make a shopping cart so that the customer has on the home page, options such as adding the book to the shopping list, declaring the amount of items you want to buy and with this through requisitions $_GET, the item is added to the cart using $_SESSION. At first this solution was working, but then came the biggest challenge to be developed, turning the session into cookies. Initially, I was writing some functions that were and is still working for the cart cookie, now for the wish list encounter difficulties in storing the customer's book wishes within it, mainly in relation to the attempt to delete 2 or more items from the wish list. The source code below reflects well the problem I'm going through:

conn.php

<?php
if (!isset($_COOKIE['cart'])) {
    setcookie('cart', json_encode([]), time() + 604800);
}

if (!isset($_COOKIE['desire'])) {
    setcookie('desire', json_encode([]), time() + 604800);
}

if (!isset($_COOKIE['total_value_buy'])) {
    setcookie('total_value_buy', 0, time() + 604800);
}

// CART FUNCTIONS

function addbook($id_book, $qtd) {
    $book = listbook($id_book);

    if ($qtd <= $book["number_copies"] && $qtd > 0) {
        $validation = false;
        foreach (json_decode($_COOKIE['cart']) as $key => $book) {
            if ($book->id_book == $id_book) {
                $validation = true;
                editbook($id_book, ($book->qtd_book + $qtd));
            }
        }

        if ($validation == false) {
            $book = ["id_book" => $id_book, "qtd_book" => $qtd, "vl_book" => $book["vl_book"]];
            $cart = json_decode($_COOKIE['cart']);
            array_push($cart, $book);
            setcookie('cart', json_encode($cart), time() + 604800);
        }
        return ("book added to cart");
    } else {
        return ("book not added! it is only allowed to purchase items below their available quantity in stock.");
    }
}

function editbook($id_book, $qtd) {
    foreach (json_decode($_COOKIE['cart']) as $key => $book) {
        if ($book->id_book == $id_book) {
            $book = listbook($id_book);
            if ($qtd <= $book["number_copies"] && $qtd > 0) {
                $cart = json_decode($_COOKIE['cart']);
                $cart[$key]->qtd_book = $qtd;
                $cart[$key]->vl_book = ($book["vl_book"]);
                setcookie('cart', json_encode($cart), time() + 604800);
            } else {

            }
        }
    }
}

function deletebook($index) {
    $cart = json_decode($_COOKIE['cart']);
    foreach ($cart as $key => $book) {
        if ($key == $index) {
            unset($cart[$key]);
            setcookie('cart', json_encode($cart), time() + 604800);
        }
    }
}

// WISHLIST FUNCTIONS (apparently they are cart-like functions, but it's not working properly).

function adddesire($id_book) {
    $validation = false;
    foreach (json_decode($_COOKIE['desire'], true) as $book) {
        if ($book['id_book'] == $id_book) {
            $validation = true;
            editdesire($id_book);
            return ("successfully updated");
        }
    }

    if ($validation == false) {
        $book = ["id_book" => $id_book];
        $desire = json_decode($_COOKIE['desire'], true);
        array_push($desire, $book);
        setcookie('desire', json_encode($desire), time() + 604800);
        return ("book added to wish list");
    }
}

function editdesire($id_book) {
    foreach (json_decode($_COOKIE['desire'], true) as $book) {
        if ($book['id_book'] == $id_book) {
            $desire = json_decode($_COOKIE['desire']);
            setcookie('desire', json_encode($desire), time() + 604800);
        }
    }
}

function deletedesire($index) {
    $desire = json_decode($_COOKIE['desire']);
    foreach ($desire as $key => $book) {
        if ($key == $index) {
            unset($desire[$key]);
            setcookie('desire', json_encode($desire), time() + 604800);
        }
    }
}
?>

cart.php

<?php include 'conn.php'; ?>

<?php

// CART UPDATE

if (isset($_POST['update'])) {

    // The COUNTER $e within the variables $_POST is constructed by means of a repeat loop (foreach) on the loading of the page

    for ($e = 0; $e < (int) $_POST['qtd_items']; $e++) {
        if (isset($_POST['id_book' . $e]) && $_POST['qtd_book' . $e] > 0) {
            editItem($_POST['id_book' . $e], $_POST['qtd_book' . $e]);
        } else {
            deleteItem($e);
        }
    }

    setcookie('cart', json_encode(array_values(json_decode($_COOKIE['cart']))), time() + 604800);
}

// APPLICATION OF THE COUPON ON PURCHASE

if (isset($_POST['apply_coupon']) || isset($_COOKIE['user_coupon'])) {
    for ($e = 0; $e < (int) isset($_POST['qtd_items']) ? $_POST['qtd_items'] : 0; $e++) {
        if (isset($_POST['id_book' . $e]) && $_POST['qtd_book' . $e] > 0) {
            editItem($_POST['id_book' . $e], $_POST['qtd_book' . $e]);
        } else {
            deleteItem($e);
        }
    }
    
    if (isset($_POST['apply_coupon'])) {
        $coupon = listcoupon2($_POST['cd_coupon'])->fetch_all(MYSQLI_ASSOC);
        setcookie('user_coupon', $_POST['cd_coupon'], time() + 604800);
        
    } else if (isset($_COOKIE['user_coupon'])) {
        $coupon = listcoupon2($_COOKIE['user_coupon'])->fetch_all(MYSQLI_ASSOC);
        setcookie('user_coupon', isset($coupon[0]['cd_coupon']) ? $coupon[0]['cd_coupon'] : 0, time() + 604800);
    } else {
        setcookie('user_coupon', (isset($coupon['cd_coupon'])) ? $coupon['cd_coupon'] : $_COOKIE['user_coupon'], time() + 604800);
    }
    
}

// CART UPDATE AND PURCHASE COMPLETION

if (isset($_POST['buy'])) {
    for ($e = 0; $e < (int) $_POST['qtd_items']; $e++) {
        if (isset($_POST['id_book' . $e]) && $_POST['qtd_book' . $e] > 0) {
            editItem($_POST['id_book' . $e], $_POST['qtd_book' . $e]);
        } else {
            deleteItem($e);
        }
    }

    setcookie('cart', json_encode(array_values(json_decode($_COOKIE['cart']))), time() + 604800);
    setcookie('total_value_buy', $_POST["total_value_buy"], time() + 604800);
}
?>

desire.php

<?php include 'conn.php'; ?>

<?php

// WISH LIST UPDATE

if (isset($_POST['update'])) {

    for ($e = 0; $e <= (int) $_POST['qtd_items']; $e++) {
        if (!isset($_POST['id_book' . $e])) {
            $desires = json_decode($_COOKIE['desire'], true);
            // ERASING ITEMS FROM THE LIST
            foreach ($desires as $key => $item) {
                if ($key == $e) {
                    unset($desires[$key]);
                    setcookie('desire', json_encode($desires), time() + 604800);
                }
            }
            echo "erased<hr>";
        }
    }
}
?>

What would be the best alternative to solve this problem in the PHP language? I have tried to look for other alternatives in

how-to-unset-a-json-object,
cannot-use-object-of-type-stdclass-as-array,
how-to-convert-an-array-into-an-object-using-stdclass,
how-to-use-array-push-for-json-encode,
arrays-in-cookies-php

... but the code continues to generate error.

Dharman
  • 30,962
  • 25
  • 85
  • 135
pe.Math
  • 89
  • 1
  • 7

0 Answers0