function add_to_cart() { header('Content-Type: application/json'); $ufstore = UFStore::instance(); $errors = array(); $meta_array = array(); // Build meta data on top of this $id = $_POST['product_id']; $quantity = $_POST['product_quantity']; //Validation of input if (!validProduct($id)) { array_push($errors, array('product_quantity' => 'Please enter a valid product ID')); } if (!validQuantity($quantity)) { array_push($errors, array('product_quantity' => 'Quantity must be greater than 0')); } if (!empty($errors)) { http_response_code(400); echo json_encode(array('errors' => $errors)); die; } //Generate the meta array foreach ($_POST as $key => $value) { if (strpos($key, 'meta_') === 0) { $meta_array[$key] = $value; } } $cart = UFStoreCart::addToCart($id, $quantity, $meta_array); echo json_encode(array('cart' => $cart)); die; }