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_shipping_info(context, event): assert context == event.object product = ICartProduct(context).product iproduct = IProduct(product.context) items = dict( weight_unit=iproduct.weight_unit, weight=iproduct.weight, dimension=iproduct.height * iproduct.width * iproduct.depth / 10**6, ) IAnnotations(context)['collective.cart.shipping'] = PersistentDict(items)
def update_cart(self, uid, quantity): cproduct = self.product(uid) icp = ICartProduct(cproduct) product = icp.product if product.unlimited_stock: if quantity > product.max_addable_quantity: quantity = product.max_addable_quantity cproduct.quantity = quantity cproduct.reindexObject(idxs=['quantity']) else: total_quantity = cproduct.quantity + product.stock if quantity <= icp.max_quantity: cproduct.quantity = quantity else: cproduct.quantity = icp.max_quantity cproduct.reindexObject(idxs=['quantity']) new_stock = total_quantity - cproduct.quantity product.stock = new_stock cproduct.subtotal = cproduct.price * cproduct.quantity
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
def products(self): context = aq_inner(self.context) cart = IPortal(context).cart if cart is not None: products = ICart(cart).products if products: properties = getToolByName(context, 'portal_properties') pcp = IPortalCartProperties(properties) res = [] for product in products: cproduct = ICartProduct(product) item = dict( uid=product.uid, title=product.title, quantity=product.quantity, url=cproduct.product.url, price_with_currency=pcp.price_with_currency( cproduct.price), html_quantity=cproduct.html_quantity, subtotal_with_currency=pcp.price_with_currency( cproduct.subtotal), ) res.append(item) return res
def subtotal(self): prices = [ICartProduct(product).subtotal for product in self.products] return sum(prices)