|
@@ -5,7 +5,8 @@ import shortuuid
|
|
|
|
|
|
|
|
from app.schema import CartUpdate, DocumentProduct, 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, get_products_id_db
|
|
|
|
|
+from app.parser import parse_file
|
|
|
|
|
|
|
|
|
|
|
|
|
def product_exist_in_cart(
|
|
def product_exist_in_cart(
|
|
@@ -45,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, settings) -> 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 products 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
|
|
@@ -53,23 +54,18 @@ def get_inventory_amount(session_item, products) -> int | None:
|
|
|
"""
|
|
"""
|
|
|
inventory_amount = None
|
|
inventory_amount = None
|
|
|
|
|
|
|
|
- for product in products:
|
|
|
|
|
- if product.meta.product_id == session_item["product_id"]:
|
|
|
|
|
- for inventory in product.meta.inventory:
|
|
|
|
|
- if (
|
|
|
|
|
- inventory.size == session_item["options"]["size"]
|
|
|
|
|
- and inventory.style == session_item["options"]["style"]
|
|
|
|
|
- ):
|
|
|
|
|
- inventory_amount = inventory.amount
|
|
|
|
|
|
|
|
|
|
- if inventory_amount:
|
|
|
|
|
- return inventory_amount
|
|
|
|
|
|
|
+ product = parse_file(settings.git_repo, Path(products["products"][session_item["product_id"]]), settings.block_types)
|
|
|
|
|
+
|
|
|
|
|
+ for variation in product.meta.inventory:
|
|
|
|
|
+ if variation.product_variation_id == session_item["options"]["product_variation_id"]:
|
|
|
|
|
+ return variation.amount
|
|
|
|
|
|
|
|
|
|
|
|
|
def update_cart_meta(
|
|
def update_cart_meta(
|
|
|
session,
|
|
session,
|
|
|
shipping_amount: int,
|
|
shipping_amount: int,
|
|
|
- products: list[DocumentProduct] | None = None
|
|
|
|
|
|
|
+ settings,
|
|
|
) -> NoReturn:
|
|
) -> NoReturn:
|
|
|
"""Update Meta key of Session Cart (subtotal, shipping, total).
|
|
"""Update Meta key of Session Cart (subtotal, shipping, total).
|
|
|
|
|
|
|
@@ -81,6 +77,7 @@ def update_cart_meta(
|
|
|
session_meta_item_amount = 0
|
|
session_meta_item_amount = 0
|
|
|
session_meta_subtotal = 0
|
|
session_meta_subtotal = 0
|
|
|
session_meta_weight = 0
|
|
session_meta_weight = 0
|
|
|
|
|
+ products = get_products_id_db(settings)
|
|
|
|
|
|
|
|
for item in session["items"].values():
|
|
for item in session["items"].values():
|
|
|
if products is not None:
|
|
if products is not None:
|
|
@@ -90,7 +87,7 @@ def update_cart_meta(
|
|
|
# - or adjust user's cart to the max available number of
|
|
# - or adjust user's cart to the max available number of
|
|
|
# items set in the inventory of the given item
|
|
# items set in the inventory of the given item
|
|
|
|
|
|
|
|
- inventory_amount = get_inventory_amount(item, products)
|
|
|
|
|
|
|
+ inventory_amount = get_inventory_amount(item, products, settings)
|
|
|
|
|
|
|
|
if inventory_amount == 0:
|
|
if inventory_amount == 0:
|
|
|
# if inventory amount for given item has meanwhile gone to 0,
|
|
# if inventory amount for given item has meanwhile gone to 0,
|
|
@@ -146,6 +143,7 @@ def update_cart(
|
|
|
shipping_amount: int,
|
|
shipping_amount: int,
|
|
|
quantity: int,
|
|
quantity: int,
|
|
|
inventory_amount: int,
|
|
inventory_amount: int,
|
|
|
|
|
+ settings,
|
|
|
) -> NoReturn:
|
|
) -> NoReturn:
|
|
|
"""Update (add / remove) Session Cart with given product_id + options
|
|
"""Update (add / remove) Session Cart with given product_id + options
|
|
|
item: if Cart contains the product, increase / decrease product's
|
|
item: if Cart contains the product, increase / decrease product's
|
|
@@ -212,7 +210,7 @@ def update_cart(
|
|
|
|
|
|
|
|
session["items"][product_id] = update_data
|
|
session["items"][product_id] = update_data
|
|
|
|
|
|
|
|
- update_cart_meta(session, shipping_amount)
|
|
|
|
|
|
|
+ update_cart_meta(session, shipping_amount, settings)
|
|
|
|
|
|
|
|
|
|
|
|
|
def clear_cart(session, currency: str) -> NoReturn:
|
|
def clear_cart(session, currency: str) -> NoReturn:
|
|
@@ -263,9 +261,10 @@ def create_product_list(settings: Settings, cart_items: dict[CartSessionItem]) -
|
|
|
"""
|
|
"""
|
|
|
|
|
|
|
|
product_list = []
|
|
product_list = []
|
|
|
|
|
+ products = get_products_id_db()
|
|
|
|
|
|
|
|
for item in cart_items.values():
|
|
for item in cart_items.values():
|
|
|
- product = get_product_by_product_id(settings, item.product_id)
|
|
|
|
|
|
|
+ product = get_product_by_product_id(settings, item.product_id, products)
|
|
|
|
|
|
|
|
product_path = str(Path(product.meta.path).stem)
|
|
product_path = str(Path(product.meta.path).stem)
|
|
|
item = ProductInfoCode(path=product_path,
|
|
item = ProductInfoCode(path=product_path,
|