%PDF- %GIF98; %PNG;
Server : ApacheSystem : 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/www/website_6ca89795/api/ |
Upload File : |
<?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);
$product_id = intval(
$_REQUEST['product_id']
?? $json['product_id']
?? 0
);
$quantity = intval(
$_REQUEST['quantity']
?? $json['quantity']
?? 1
);
/*
|--------------------------------------------------------------------------
| Validation
|--------------------------------------------------------------------------
*/
if ($product_id <= 0) {
response(false, "Product ID Required", [
"required_fields" => [
"product_id"
],
"example_url" =>
BASE_URL .
"api/add_to_cart.php?product_id=1&quantity=1",
"form_data" => [
[
"key" => "product_id",
"value" => "1"
],
[
"key" => "quantity",
"value" => "1"
]
],
"raw_json" => [
"product_id" => 1,
"quantity" => 1
]
]);
}
if ($quantity <= 0) {
$quantity = 1;
}
/*
|--------------------------------------------------------------------------
| Check Product
|--------------------------------------------------------------------------
*/
$productQry = mysqli_query(
$conn,
"SELECT * FROM areawecover
WHERE id='$product_id'
AND status='1'"
);
if (mysqli_num_rows($productQry) == 0) {
response(false, "Product Not Found");
}
$product = mysqli_fetch_assoc($productQry);
/*
|--------------------------------------------------------------------------
| Stock Validation
|--------------------------------------------------------------------------
*/
$stock = (int)$product['stock'];
if ($stock <= 0) {
response(false, "Product Out Of Stock");
}
if ($quantity > $stock) {
response(false, "Only ".$stock." items available", [
"available_stock" => $stock
]);
}
/*
|--------------------------------------------------------------------------
| Offer Calculation
|--------------------------------------------------------------------------
*/
$price = (float)$product['price'];
$actual_price = (float)$product['actual_price'];
$offer_percentage = 0;
if (
$actual_price > 0 &&
$actual_price > $price
) {
$offer_percentage = round(
(
($actual_price - $price)
/
$actual_price
) * 100
);
}
/*
|--------------------------------------------------------------------------
| Check Existing Cart Item
|--------------------------------------------------------------------------
*/
$checkCart = mysqli_query(
$conn,
"SELECT *
FROM cart_items
WHERE user_id='$user_id'
AND product_id='$product_id'"
);
if (mysqli_num_rows($checkCart) > 0) {
$cartRow = mysqli_fetch_assoc($checkCart);
$newQty =
$cartRow['quantity'] +
$quantity;
if ($newQty > $stock) {
response(false, "Only ".$stock." items available", [
"available_stock" => $stock
]);
}
mysqli_query(
$conn,
"UPDATE cart_items
SET quantity='$newQty'
WHERE id='".$cartRow['id']."'"
);
response(true, "Cart Updated Successfully", [
"user_id" => $user_id,
"product_id" => $product_id,
"quantity" => $newQty,
"product" => [
"id" => $product['id'],
"title" => $product['title'],
"price" => $price,
"actual_price" => $actual_price,
"offer_percentage" => $offer_percentage,
"currency_symbol" => $currency_symbol,
"currency_code" => $currency_code,
"stock" => $stock
],
"cart_api" =>
BASE_URL . "api/cart.php"
]);
}
/*
|--------------------------------------------------------------------------
| Insert New Cart Item
|--------------------------------------------------------------------------
*/
mysqli_query(
$conn,
"INSERT INTO cart_items
(
user_id,
product_id,
quantity
)
VALUES
(
'$user_id',
'$product_id',
'$quantity'
)"
);
$cart_id = mysqli_insert_id($conn);
response(true, "Product Added To Cart", [
"cart_id" => $cart_id,
"user_id" => $user_id,
"product" => [
"id" => $product['id'],
"title" => $product['title'],
"price" => $price,
"actual_price" => $actual_price,
"offer_percentage" => $offer_percentage,
"currency_symbol" => $currency_symbol,
"currency_code" => $currency_code,
"stock" => $stock
],
"quantity" => $quantity,
"next_api" => [
"cart_api" =>
BASE_URL . "api/cart.php"
]
]);
$conn->close();
?>