I have a service that looks like this (You can see it's from a shopping cart.)
I am trying to reset/remove this cart with a button (Buy More New Items"), to make a new order, but the quantity of the before order it continues, and whenever it makes a new buy order, sum with the previous order quantity.I am trying to reset/remove this cart with a button (Buy More New Items"), to make a new order, but the quantity of the before order it continues, and whenever it makes a new buy order, sum with the previous order quantity.
It's The Service so the correct is to say at the end of it uses:
angular.module('nhaac.services', [])
.factory('sharedCartService', ['$ionicPopup',function($ionicPopup){
// OKAY, MAS ONDE ELE ESTÁ PEGANDO?
// DECLARA AS VARIÁVEIS
var cartObj = {};
cartObj.cart=[];
cartObj.total_amount=0;
cartObj.total_qty=0;
// VERIFICA DE JÁ EXISTE ITENS NO CARRINHO
cartObj.cart.add=function(id,image,name,price,qty,supply_id,deliver){
if( cartObj.cart.find(id)!=-1 ){
var alertPopup = $ionicPopup.alert({
title: 'Este produto já foi Adicionado',
template: 'Acrescente mais quantidade abaixo da oferta no carrinho'
});
// }if (CartObj, { 'cart_item_supply': supply_id}) {
}if (cartObj.cart.findvovo(supply_id) !=-1 ){
var alertPopup = $ionicPopup.alert({
title: "Você só pode comprar de uma única Vovó por vez",
template: "Volte ao carrinho e escolha a opção COMPRAR MAIS DELICIAS."
});
}else{
cartObj.cart.push( { "cart_item_id": id , "cart_item_image": image , "cart_item_name": name , "cart_item_price": price , "cart_item_qty": qty, "cart_item_supply": supply_id, "cart_item_deliver": deliver } );
cartObj.total_qty+=1;
cartObj.total_amount+=parseInt(price);
console.log(cartObj);
}
};
// PROCURA PRODUTOS NO CARRINHO PELO ID
cartObj.cart.find=function(id){
var result=-1;
for( var i = 0, len = cartObj.cart.length; i < len; i++ ) {
if( cartObj.cart[i].cart_item_id === id ) {
result = i;
console.log(result);
break;
}
}
return result;
};
// PROCURA COD FORNECEDOR É IGUAL
cartObj.cart.findvovo=function(supply_id){
var result=-1;
for( var i = 0, len = cartObj.cart.length; i < len; i++ ) {
if( cartObj.cart[i].cart_item_supply === supply_id ) {
result = i;
console.log('Qual Vovo achou '+result);
break;
}
};
return result;
};
// CLEAN CART
cartObj.cart.drop=function(id){
var temp=cartObj.cart[cartObj.cart.find(id)];
cartObj.total_qty-= parseInt(temp.cart_item_qty);
cartObj.total_amount-=( parseInt(temp.cart_item_qty) * parseInt(temp.cart_item_price) );
window.localStorage.removeItem("fonecedor_carrinho",cartObj.supply_id);
cartObj.cart.splice(cartObj.cart.find(id), 1);
};
// ADD ITENS TO CART
cartObj.cart.increment=function(id){
cartObj.cart[cartObj.cart.find(id)].cart_item_qty+=1;
cartObj.total_qty+= 1;
cartObj.total_amount+=( parseInt( cartObj.cart[cartObj.cart.find(id)].cart_item_price) );
};
// DIMINUI A QUANTIDADE DE ITENS NO CARRINHO
cartObj.cart.decrement=function(id){
cartObj.total_qty-= 1;
cartObj.total_amount-= parseInt( cartObj.cart[cartObj.cart.find(id)].cart_item_price) ;
if(cartObj.cart[cartObj.cart.find(id)].cart_item_qty == 1){ // if the cart item was only 1 in qty
cartObj.cart.splice( cartObj.cart.find(id) , 1); //edited
}else{
cartObj.cart[cartObj.cart.find(id)].cart_item_qty-=1;
}
};
console.log('chama return');
return cartObj;
}])
You can see it's from a shopping cart.
I'm trying to delete this service like this:
$scope.novoPedido = function () {
// CLEAN CART SHOP
var cart = sharedCartService.cart;
sharedCartService.cart.splice(0, sharedCartService.cart.length);
// sharedCartService.cart.drop("");
>> This dont work
// $state.go('novopedido');
};
But it does not delete the quantity, it just clears the items, the quantity continues, how to delete everything, including the quantity? I do not know how to do it ...
Thank you!
Resolved with:
var cart = sharedCartService.cart;
sharedCartService.cart.splice(0, sharedCartService.cart.length);
sharedCartService.total_qty = 0;
sharedCartService.total_amount = 0;