CameraBuy / web / assets / script / cart.js
cart.js
Raw
//this function adds an item to the cart param: itemid ;
function addtocart(item){
    fetch('/api/cart.php', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify(
            {"add": item}
            ),
      })
      .then(response => response.json())
      .then(data => {
        console.log('Response from server:', data);
      })
      .catch(error => {
        console.error('Error:'+ error);
      });
    //the updatecart function does different things depending from wich page it is called
    updatecart(item)
}

//this function subtracts an item from the cart; param: itemid ;
function subtractfromcart(item){
  fetch('/api/cart.php', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(
          {"subtract": item}
          ),
    })
    .then(response => response.json())
    .then(data => {
      console.log('Response from server:', data);
    })
    //the updatecart function does different things depending from wich page it is called
    updatecart(item)
}

//this function removes an item to the cart; param: itemid ;
function removefromcart(item){
    fetch('/api/cart.php', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify(
            {"remove": item}
            ),
      })
      .then(response => response.json())
      .then(data => {
        console.log('Response from server:', data);
      })
      .catch(error => {
        console.error('Error:'+ error);
      });
    //the updatecart function does different things depending from wich page it is called
    updatecart()
}

//this function returns the item a user has in cart; param: - ; return: itemids: array;
async function printcart() {
  try {
    const response = await fetch('/api/cart.php', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({"print": true}),
    });

    if (!response.ok) {
      throw new Error(`Fehler beim Abrufen der Daten: ${response.statusText}`);
    }

    const data = await response.json();
    return data;
  } catch (error) {
    console.error('Fehler: ' + error);
  }
}