'', 'product_ids' => '', 'discount_code' => '', 'discount_type' => 'Percentage', 'discount_value' => 0, 'start_date' => date('Y-m-d\TH:i'), 'end_date' => date('Y-m-d\TH:i', strtotime('+1 month', strtotime(date('Y-m-d\TH:i')))), 'categories' => [], 'products' => [] ]; $types = ['Percentage', 'Fixed']; // Get all the categories from the database $stmt = $pdo->query('SELECT * FROM categories'); $stmt->execute(); $categories = $stmt->fetchAll(PDO::FETCH_ASSOC); // Get all the products from the database $stmt = $pdo->query('SELECT * FROM products'); $stmt->execute(); $products = $stmt->fetchAll(PDO::FETCH_ASSOC); if (isset($_GET['id'])) { // ID param exists, edit an existing discount $page = 'Edit'; if (isset($_POST['submit'])) { // Update the discount $categories_list = isset($_POST['categories']) ? implode(',', $_POST['categories']) : ''; $products_list = isset($_POST['products']) ? implode(',', $_POST['products']) : ''; $stmt = $pdo->prepare('UPDATE discounts SET category_ids = ?, product_ids = ?, discount_code = ?, discount_type = ?, discount_value = ?, start_date = ?, end_date = ? WHERE id = ?'); $stmt->execute([ $categories_list, $products_list, $_POST['discount_code'], $_POST['discount_type'], $_POST['discount_value'], date('Y-m-d H:i:s', strtotime($_POST['start_date'])), date('Y-m-d H:i:s', strtotime($_POST['end_date'])), $_GET['id'] ]); // Remove session discount code if (isset($_SESSION['discount'])) { unset($_SESSION['discount']); } header('Location: index.php?page=discounts&success_msg=2'); exit; } if (isset($_POST['delete'])) { // Delete the discount $stmt = $pdo->prepare('DELETE FROM discounts WHERE id = ?'); $stmt->execute([ $_GET['id'] ]); // Remove session discount code if (isset($_SESSION['discount'])) { unset($_SESSION['discount']); } header('Location: index.php?page=discounts&success_msg=3'); exit; } // Get the discount from the database $stmt = $pdo->prepare('SELECT * FROM discounts WHERE id = ?'); $stmt->execute([ $_GET['id'] ]); $discount = $stmt->fetch(PDO::FETCH_ASSOC); // Get the discount categories $stmt = $pdo->prepare('SELECT c.name, c.id FROM discounts d JOIN categories c ON FIND_IN_SET(c.id, d.category_ids) WHERE d.id = ?'); $stmt->execute([ $_GET['id'] ]); $discount['categories'] = $stmt->fetchAll(PDO::FETCH_ASSOC); // Get the discount products $stmt = $pdo->prepare('SELECT p.name, p.id FROM discounts d JOIN products p ON FIND_IN_SET(p.id, d.product_ids) WHERE d.id = ?'); $stmt->execute([ $_GET['id'] ]); $discount['products'] = $stmt->fetchAll(PDO::FETCH_ASSOC); } else { // Create a new discount $page = 'Create'; if (isset($_POST['submit'])) { $categories_list = isset($_POST['categories']) ? implode(',', $_POST['categories']) : ''; $products_list = isset($_POST['products']) ? implode(',', $_POST['products']) : ''; $stmt = $pdo->prepare('INSERT INTO discounts (category_ids,product_ids,discount_code,discount_type,discount_value,start_date,end_date) VALUES (?,?,?,?,?,?,?)'); $stmt->execute([ $categories_list, $products_list, $_POST['discount_code'], $_POST['discount_type'], $_POST['discount_value'], date('Y-m-d H:i:s', strtotime($_POST['start_date'])), date('Y-m-d H:i:s', strtotime($_POST['end_date'])) ]); // Remove session discount code if (isset($_SESSION['discount'])) { unset($_SESSION['discount']); } header('Location: index.php?page=discounts&success_msg=1'); exit; } } ?>

Discount

Cancel
×
×