def add_new_product_to_cart(self, uid, quantity): pid = '1' if self.products is not None: ids = [product.id for product in self.products] for r in range(1, len(ids) + 2): pid = str(r) if pid not in ids: pid = pid break self.context.invokeFactory( 'CartProduct', pid, ) cproduct = self.context[pid] cproduct.uid = uid max_quantity = ICartProduct(cproduct).max_quantity if quantity > max_quantity: quantity = max_quantity cproduct.quantity = quantity product = ICartProduct(cproduct).product cproduct.price = product.price cproduct.title = product.title cproduct.subtotal = cproduct.price * cproduct.quantity cproduct.reindexObject() if not product.unlimited_stock: new_stock = product.stock - quantity product.stock = new_stock notify(ObjectInitializedEvent(cproduct))
def delete_product(self, uid): cproduct = self.product(uid) quantity = cproduct.quantity product = ICartProduct(cproduct).product if not product.unlimited_stock: new_stock = product.stock + quantity product.stock = new_stock cproduct.unindexObject() del self.context[cproduct.id]
def add_existing_product_to_cart(self, uid, quantity): cproduct = self.product(uid) product = ICartProduct(cproduct).product total_quantity = product.stock + cproduct.quantity max_quantity = ICartProduct(cproduct).max_quantity new_quantity = cproduct.quantity + quantity if new_quantity > max_quantity: new_quantity = max_quantity cproduct.quantity = new_quantity cproduct.subtotal = cproduct.price * cproduct.quantity cproduct.reindexObject(idxs=['quantity']) product = ICartProduct(cproduct).product if not product.unlimited_stock: new_stock = total_quantity - new_quantity product.stock = new_stock