Home > Blockchain >  Calculating total price present in shopping cart
Calculating total price present in shopping cart

Time:09-22

This is how my shopping cart looks like..

I am trying to calculate the subtotal price based on the products present in the cart dynamically. My code is here :

    func displaySubTotal() {
        
        if cartArray.count == 1 {
            let p1 = Double(cartArray[0].cartItems.price)
            let p2 = Double(cartArray[0].cartQuantity)
            let p = Double(p1! * p2)
            
            let x1 = Double(p)
            subtotalPrice.text = "\(x1)"
        }

        if cartArray.count == 2 {
            
            let p1 = Double(cartArray[0].cartItems.price)
            let p2 = Double(cartArray[0].cartQuantity)
            let p = Double(p1! * p2)
            
            let q1 = Double(cartArray[1].cartItems.price)
            let q2 = Double(cartArray[1].cartQuantity)
            let q = Double(q1! * q2)
            
            let x2 = Double(p   q)
            subtotalPrice.text = "\(x2)"
        }
        
        if cartArray.count == 3 {
            
            let p1 = Double(cartArray[0].cartItems.price)
            let p2 = Double(cartArray[0].cartQuantity)
            let p = Double(p1! * p2)
            
            let q1 = Double(cartArray[1].cartItems.price)
            let q2 = Double(cartArray[1].cartQuantity)
            let q = Double(q1! * q2)
        
            let r1 = Double(cartArray[2].cartItems.price)
            let r2 = Double(cartArray[2].cartQuantity)
            let r = Double(r1! * r2)
            
            let x3 = Double(p   q   r)
            subtotalPrice.text = "\(x3)"
        }
        
        if cartArray.count == 4 {
            
            let p1 = Double(cartArray[0].cartItems.price)
            let p2 = Double(cartArray[0].cartQuantity)
            let p = Double(p1! * p2)
            
            let q1 = Double(cartArray[1].cartItems.price)
            let q2 = Double(cartArray[1].cartQuantity)
            let q = Double(q1! * q2)
        
            let r1 = Double(cartArray[2].cartItems.price)
            let r2 = Double(cartArray[2].cartQuantity)
            let r = Double(r1! * r2)
            
            let s1 = Double(cartArray[3].cartItems.price)!
            let s2 = Double(cartArray[3].cartQuantity)
            let s = Double(s1 * s2)
            
            let x4 = Double(p   q   r   s)
            subtotalPrice.text = "\(x4)"
        }
        
        if cartArray.count == 5 {
            
            let p1 = Double(cartArray[0].cartItems.price)
            let p2 = Double(cartArray[0].cartQuantity)
            let p = Double(p1! * p2)
            
            let q1 = Double(cartArray[1].cartItems.price)
            let q2 = Double(cartArray[1].cartQuantity)
            let q = Double(q1! * q2)
        
            let r1 = Double(cartArray[2].cartItems.price)
            let r2 = Double(cartArray[2].cartQuantity)
            let r = Double(r1! * r2)
            
            let s1 = Double(cartArray[3].cartItems.price)!
            let s2 = Double(cartArray[3].cartQuantity)
            let s = Double(s1 * s2)
            
            let t1 = Double(cartArray[4].cartItems.price)!
            let t2 = Double(cartArray[4].cartQuantity)
            let t = Double(t1 * t2)
            
            let x5 = Double(p   q   r   s   t)
            subtotalPrice.text = "\(x5)"
        }
    }

But I can't make it dynamic (price is needed to be updated with the delete of products instantly & addition/subtraction of quantity [using the ( & -) buttons] of products). I'm trying this kind of thing for the first time. Please suggest me hoe to solve this! Thanks in advance..

CodePudding user response:

Just loop over your cart items. (item_price * quantity)  

func displaySubTotal() {
  var total_price: Float = 0.0
    for items in cartArray {
        if let price = Float(items.cartItems.price) {
            total_price  = Float(items.cartQuantity) * price
        }
    }
    subtotalPrice.text = "\(total_price)"
}

@objc func add(sender: UIButton) {
    if cartArray[sender.tag].cartQuantity >= 0 {
        cartArray[sender.tag].cartQuantity  = 1
        cartTableView.reloadData()
        self.displaySubTotal()
    }
 }

@objc func sub(sender: UIButton) {
    if cartArray[sender.tag].cartQuantity > 0 {
        cartArray[sender.tag].cartQuantity -= 1
        cartTableView.reloadData()
        self.displaySubTotal()
    }
}
  • Related