0

I want to add items to cart and display total sum. Here I created a function addTo() with a purpose to add each item and then I created function getCost() to multiple price and num of the item. Then I want to display total sum automatically every time when I click 'add to cart'. Please, help me to build this function. I have no idea.

angular.module('TransactionApp', [])
.controller('TransactionsCtrl', function($scope) {

   $scope.title = 'Online-store';

   $scope.itemsArray = [
    {  price: 50, name: "Whey protein", img: 'img/item-1.png', quantity: 0},
    {  price: 60, name: "Protein bar", img: 'img/item-1.png', quantity: 0  },
    {  price: 35, name: "BCAA", img: 'img/item-1.png', quantity: 0  },
    {  price: 50, name: "Whey protein", img: 'img/item-1.png', quantity: 0  },
    {  price: 60, name: "Protein bar", img: 'img/item-1.png', quantity: 0  },
    {  price: 80, name: "BCAA", img: 'img/item-1.png', quantity: 0  }

   ];

   $scope.addTo = function(item) {
       item.quantity += 1;
     }
     $scope.getCost = function(item) {
      return item.quantity * item.price;

     }

   $scope.cart = [];

   $scope.getTotal = function() {

   }


});
Oksana Shukh
  • 237
  • 3
  • 12
  • addTo() works when I press 'add to cart' button – Oksana Shukh Jul 18 '18 at 07:46
  • 1
    That's nice to know, but what should we do with this information? Are you having troubles with your code? Have a look at [ask] – JSON Derulo Jul 18 '18 at 07:48
  • @D.Simon yep, I don't have an idea how to build this function – Oksana Shukh Jul 18 '18 at 16:07
  • @OksanaShukh which version of Angular are you using? – sctskw Jul 18 '18 at 16:30
  • Someone already posted a nice tutorial on how to build a shopping cart: https://stackoverflow.com/questions/47502435/how-can-i-create-a-cartdictionary-function-to-add-and-remove-items-in-javascri?rq=1 Since your question really isn't Angular specific its best to understand how to build a shopping cart without it first and apply that knowledge to what you know about Angular components/controllers – sctskw Jul 18 '18 at 16:53
  • @sctskw AngularJS – Oksana Shukh Jul 18 '18 at 18:00
  • `return $scope.itemsArray.reduce((a, b) => a + b.price * b.quantity, 0)` – Slava Utesinov Jul 19 '18 at 05:46

0 Answers0