From 87bf926c60712be2d6076c96bc9da659023f37bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=A6=8C=20=EA=A6=AB=EA=A6=B6=20=EA=A6=8F=EA=A7=80?= =?UTF-8?q?=EA=A6=A6=EA=A6=BF=20=EA=A6=A7=20=EA=A6=AE=20=EA=A6=91=20?= =?UTF-8?q?=EA=A6=A9=20=EA=A6=AD=EA=A7=80?= Date: Sat, 16 Nov 2019 11:23:43 +0700 Subject: If item is already in basket, then add the quantity instead --- basket.cxx | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/basket.cxx b/basket.cxx index b1c89f8..60ea273 100644 --- a/basket.cxx +++ b/basket.cxx @@ -1,3 +1,4 @@ +#include #include "qicclient/basket.hxx" namespace ICClient { @@ -41,10 +42,18 @@ namespace ICClient { void Basket::addItem(Item const& item) { - beginInsertRows(QModelIndex(), rowCount(), rowCount()); - items << item; - endInsertRows(); - emit rowCountChanged(); + auto product = item.product; + auto iterator = std::find_if(items.begin(), items.end() + , [&product](Item const& item) { + return product.sku == item.product.sku; + }); + if (iterator == items.end()) { + beginInsertRows(QModelIndex(), rowCount(), rowCount()); + items << item; + endInsertRows(); + emit rowCountChanged(); + } else + iterator->quantity++; } void Basket::update(icclient_ord_order* order) @@ -54,7 +63,6 @@ namespace ICClient { addItem(Item{order->items[i]}); m_subtotal = order->subtotal; m_totalCost = order->total_cost; - emit updated(); emit subtotalChanged(); emit totalCostChanged(); } -- cgit v1.2.3