fix kashkool
This commit is contained in:
+12
-20
@@ -2,8 +2,8 @@ import { computed, ref } from 'vue'
|
||||
import { defineStore } from 'pinia'
|
||||
import type { CartItem } from '@/models/cart'
|
||||
import type { Product } from '@/models/products'
|
||||
import type { Price } from '@/models/home'
|
||||
import { getProducts } from '@/api/products'
|
||||
import { getProductsCatalog } from '@/composables/useProductsCatalog'
|
||||
import { getProductBuyPrice } from '@/utils/productPrice'
|
||||
|
||||
const CART_STORAGE_KEY = 'ghatre_cart'
|
||||
|
||||
@@ -113,23 +113,13 @@ export const useCartStore = defineStore('cart', () => {
|
||||
return beforeCount - cartItems.value.length
|
||||
}
|
||||
|
||||
function recalculateCartPrices(lastPrice: Price) {
|
||||
if (!lastPrice || cartItems.value.length === 0) return
|
||||
function updateCartItemPrices() {
|
||||
if (cartItems.value.length === 0) return
|
||||
|
||||
cartItems.value.forEach((item) => {
|
||||
const product = item.product
|
||||
let newPrice = 0
|
||||
|
||||
if (product.group_type == 'آبشده') {
|
||||
newPrice = lastPrice.price_last ?? 0
|
||||
} else {
|
||||
newPrice = product.amount * (lastPrice.price_last ?? 0)
|
||||
}
|
||||
|
||||
product.price = newPrice
|
||||
product.sellPrice = product.amount * (lastPrice.sell ?? 0)
|
||||
item.price = newPrice + (product.cash_fee_buy ?? 0)
|
||||
item.totalPrice = parseInt(String(item.price * item.quantity))
|
||||
const buyPrice = getProductBuyPrice(item.product)
|
||||
item.price = buyPrice
|
||||
item.totalPrice = parseInt(String(buyPrice * item.quantity))
|
||||
})
|
||||
|
||||
saveCartToStorage()
|
||||
@@ -139,8 +129,10 @@ export const useCartStore = defineStore('cart', () => {
|
||||
if (cartItems.value.length === 0) return 0
|
||||
|
||||
try {
|
||||
const serverProducts: Product[] = await getProducts()
|
||||
return syncProductsFromServer(serverProducts)
|
||||
const serverProducts: Product[] = await getProductsCatalog()
|
||||
const removed = syncProductsFromServer(serverProducts)
|
||||
updateCartItemPrices()
|
||||
return removed
|
||||
} catch (e) {
|
||||
console.error('Error refreshing cart from server:', e)
|
||||
return 0
|
||||
@@ -158,7 +150,7 @@ export const useCartStore = defineStore('cart', () => {
|
||||
loadCartFromStorage,
|
||||
saveCartToStorage,
|
||||
syncProductsFromServer,
|
||||
recalculateCartPrices,
|
||||
updateCartItemPrices,
|
||||
refreshCartFromServer
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user