|
@@ -3,7 +3,7 @@ from typing import NoReturn
|
|
|
|
|
|
|
|
import shortuuid
|
|
import shortuuid
|
|
|
|
|
|
|
|
-from app.schema import CartUpdate, DocumentProduct, create_product_id, CartSessionItem, Settings, ProductInfoCode
|
|
|
|
|
|
|
+from app.schema import CartUpdate, DocumentProduct, CartSessionItem, Settings, ProductInfoCode
|
|
|
from app.template import make_product_info_code
|
|
from app.template import make_product_info_code
|
|
|
from app.db import get_product_by_product_id
|
|
from app.db import get_product_by_product_id
|
|
|
|
|
|
|
@@ -12,18 +12,18 @@ def product_exist_in_cart(
|
|
|
product_id: str,
|
|
product_id: str,
|
|
|
size: str | None,
|
|
size: str | None,
|
|
|
style: str | None,
|
|
style: str | None,
|
|
|
|
|
+ product_variation_id: str | None,
|
|
|
session_cart_items: dict[str, dict[str, str | dict[str, str]]],
|
|
session_cart_items: dict[str, dict[str, str | dict[str, str]]],
|
|
|
) -> bool:
|
|
) -> bool:
|
|
|
"""Check if given product exists in the cart already by constructing
|
|
"""Check if given product exists in the cart already by constructing
|
|
|
a full label using the product's id and eventual size and style
|
|
a full label using the product's id and eventual size and style
|
|
|
options.
|
|
options.
|
|
|
"""
|
|
"""
|
|
|
- product_id_label = create_product_id(product_id, size, style)
|
|
|
|
|
|
|
|
|
|
- if product_id_label in session_cart_items:
|
|
|
|
|
|
|
+ if product_id in session_cart_items or product_variation_id in session_cart_items:
|
|
|
return True
|
|
return True
|
|
|
- else:
|
|
|
|
|
- return False
|
|
|
|
|
|
|
+
|
|
|
|
|
+ return False
|
|
|
|
|
|
|
|
|
|
|
|
|
def initialize_cart(session, currency: str) -> dict[int, str] | NoReturn:
|
|
def initialize_cart(session, currency: str) -> dict[int, str] | NoReturn:
|
|
@@ -46,7 +46,7 @@ def initialize_cart(session, currency: str) -> dict[int, str] | NoReturn:
|
|
|
|
|
|
|
|
def get_inventory_amount(session_item, products) -> int | None:
|
|
def get_inventory_amount(session_item, products) -> int | None:
|
|
|
"""Returns the inventory amount value for any product by matching it
|
|
"""Returns the inventory amount value for any product by matching it
|
|
|
- against the list of selected producs in the cart. We need to make
|
|
|
|
|
|
|
+ against the list of selected products in the cart. We need to make
|
|
|
use of the selected product's style and size options in order to
|
|
use of the selected product's style and size options in order to
|
|
|
retrieve the correct inventory amount value.
|
|
retrieve the correct inventory amount value.
|
|
|
"""
|
|
"""
|
|
@@ -108,9 +108,10 @@ def update_cart_meta(
|
|
|
session_meta_subtotal += item_quantity * item["price"]
|
|
session_meta_subtotal += item_quantity * item["price"]
|
|
|
|
|
|
|
|
# -- update also each item quantity and availability under session.cart.items
|
|
# -- update also each item quantity and availability under session.cart.items
|
|
|
- cart_product_id = create_product_id(
|
|
|
|
|
- item["product_id"], item["options"]["size"], item["options"]["style"]
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ if item["options"]["product_variation_id"]:
|
|
|
|
|
+ cart_product_id = item["options"]["product_variation_id"]
|
|
|
|
|
+ else:
|
|
|
|
|
+ cart_product_id = item["product_id"]
|
|
|
|
|
|
|
|
session["items"][cart_product_id]["quantity"] = item_quantity
|
|
session["items"][cart_product_id]["quantity"] = item_quantity
|
|
|
session["items"][cart_product_id]["availability"] = inventory_amount > 0
|
|
session["items"][cart_product_id]["availability"] = inventory_amount > 0
|
|
@@ -145,9 +146,11 @@ def update_cart(
|
|
|
item: if Cart contains the product, increase / decrease product's
|
|
item: if Cart contains the product, increase / decrease product's
|
|
|
quantity, else add / remove the product to / from the Cart.
|
|
quantity, else add / remove the product to / from the Cart.
|
|
|
"""
|
|
"""
|
|
|
- product_id = create_product_id(
|
|
|
|
|
- form.product_id, form.options.size, form.options.style
|
|
|
|
|
- )
|
|
|
|
|
|
|
+
|
|
|
|
|
+ if form.options.product_variation_id:
|
|
|
|
|
+ product_id = form.options.product_variation_id # variation ID is used for item session storage if it exists
|
|
|
|
|
+ else:
|
|
|
|
|
+ product_id = form.product_id
|
|
|
|
|
|
|
|
if product_exists:
|
|
if product_exists:
|
|
|
# get currently updated product
|
|
# get currently updated product
|
|
@@ -189,9 +192,10 @@ def update_cart(
|
|
|
else:
|
|
else:
|
|
|
if form.operation == "add":
|
|
if form.operation == "add":
|
|
|
if quantity <= inventory_amount:
|
|
if quantity <= inventory_amount:
|
|
|
- session["items"][product_id] = {
|
|
|
|
|
|
|
+ update_data = {
|
|
|
"product_id": form.product_id,
|
|
"product_id": form.product_id,
|
|
|
"options": {
|
|
"options": {
|
|
|
|
|
+ "product_variation_id": form.options.product_variation_id,
|
|
|
"size": form.options.size,
|
|
"size": form.options.size,
|
|
|
"style": form.options.style,
|
|
"style": form.options.style,
|
|
|
},
|
|
},
|
|
@@ -200,6 +204,8 @@ def update_cart(
|
|
|
"availability": inventory_amount > 0,
|
|
"availability": inventory_amount > 0,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ session["items"][product_id] = update_data
|
|
|
|
|
+
|
|
|
update_cart_meta(session, shipping_amount)
|
|
update_cart_meta(session, shipping_amount)
|
|
|
|
|
|
|
|
|
|
|
|
@@ -224,8 +230,6 @@ def clear_cart(session, currency: str) -> NoReturn:
|
|
|
def calculate_quantity_requested_by_user(
|
|
def calculate_quantity_requested_by_user(
|
|
|
session_cart_items: dict[str, dict[str, str | dict[str, str]]],
|
|
session_cart_items: dict[str, dict[str, str | dict[str, str]]],
|
|
|
product_id: str,
|
|
product_id: str,
|
|
|
- size: str,
|
|
|
|
|
- style: str,
|
|
|
|
|
quantity: int,
|
|
quantity: int,
|
|
|
) -> int:
|
|
) -> int:
|
|
|
"""Helper function to retrieve the number of items of a specific
|
|
"""Helper function to retrieve the number of items of a specific
|
|
@@ -235,7 +239,7 @@ def calculate_quantity_requested_by_user(
|
|
|
quantity_requested_by_user = 0
|
|
quantity_requested_by_user = 0
|
|
|
|
|
|
|
|
if len(session_cart_items.keys()) > 0:
|
|
if len(session_cart_items.keys()) > 0:
|
|
|
- cart_product_id = create_product_id(product_id, size, style)
|
|
|
|
|
|
|
+ cart_product_id = product_id
|
|
|
|
|
|
|
|
if cart_product_id in session_cart_items:
|
|
if cart_product_id in session_cart_items:
|
|
|
quantity_requested_by_user = (
|
|
quantity_requested_by_user = (
|