def getItems(self): """ """ nc = queryUtility(INumberConverter) cm = ICurrencyManagement(self.context) items = [] item_manager = IItemManagement(self.context) for item in item_manager.getItems(): product_price_gross = cm.priceToString(item.getProductPriceGross(), suffix=None) tax_rate = nc.floatToTaxString(item.getTaxRate()) tax = cm.priceToString(item.getTax(), suffix=None) price_gross = cm.priceToString(item.getPriceGross(), suffix=None) # Get url. Takes care of, if the product has been deleted in the # meanwhile. product = item.getProduct() if product is None: url = None else: if IProductVariant.providedBy(product): url = product.aq_inner.aq_parent.absolute_url() else: url = product.absolute_url() # Properties for property in item.getProperties(): if IProductVariant.providedBy(product) == True: property["show_price"] = False else: property["show_price"] = True temp = { "product_title" : item.getProductTitle(), "product_quantity" : item.getProductQuantity(), "product_url" : url, "product_price_gross" : product_price_gross, "price_gross" : price_gross, "tax_rate" : tax_rate, "tax" : tax, "properties" : item.getProperties(), "has_discount" : abs(item.getDiscountGross()) > 0, "discount_description" : item.getDiscountDescription(), "discount" : cm.priceToString(item.getDiscountGross(), prefix="-", suffix=None), } items.append(temp) return items
def getItems(self): """ """ nc = queryUtility(INumberConverter) cm = ICurrencyManagement(self.context) items = [] item_manager = IItemManagement(self.context) for item in item_manager.getItems(): product_price_gross = cm.priceToString(item.getProductPriceGross(), suffix=None) tax_rate = nc.floatToTaxString(item.getTaxRate()) tax = cm.priceToString(item.getTax(), suffix=None) price_gross = cm.priceToString(item.getPriceGross(), suffix=None) # Get url. Takes care of, if the product has been deleted in the # meanwhile. product = item.getProduct() if product is None: url = None else: if IProductVariant.providedBy(product): url = product.aq_inner.aq_parent.absolute_url() else: url = product.absolute_url() # Properties for property in item.getProperties(): if IProductVariant.providedBy(product) == True: property["show_price"] = False else: property["show_price"] = True temp = { "product_title": item.getProductTitle(), "product_quantity": item.getProductQuantity(), "product_url": url, "product_price_gross": product_price_gross, "price_gross": price_gross, "tax_rate": tax_rate, "tax": tax, "properties": item.getProperties(), "has_discount": abs(item.getDiscountGross()) > 0, "discount_description": item.getDiscountDescription(), "discount": cm.priceToString(item.getDiscountGross(), prefix="-", suffix=None), } items.append(temp) return items
def _getProperties(self, cart_item): """ """ product = cart_item.getProduct() if IProductVariant.providedBy(product): return self._getPropertiesForVariants(cart_item) else: return self._getPropertiesForConfiguration(cart_item)
def getOptionsForProperty(product, property_id): """Returns all options for a given property id. """ if IProductVariant.providedBy(product) == True: product = product.aq_inner.aq_parent pm = IPropertyManagement(product) return pm.getOptionsForProperty(property_id)
def getTitlesByIds(product, property_id, option_id): # TODO: This may not be the cleanest way. Rethink it. YAGNI? """A simple wrapper to get the variants options (global options) of a variant. In this way the adapter still works for local properties (price changing) of a variant, which may later used additional to the global ones. """ if IProductVariant.providedBy(product) == True: product = product.aq_inner.aq_parent pm = IPropertyManagement(product) return pm.getTitlesByIds(property_id, option_id)
def _refreshCart(self): """ """ customer = ICustomerManagement(self.context).getAuthenticatedCustomer() cart = ICartManagement(self.context).getCart() if cart is None: return # Collect cart item properties for lookup selected_properties = {} for key, value in self.context.request.items(): if key.startswith("property_"): property_id, cart_item_id = key.split(":") property_id = property_id[42:] if selected_properties.has_key(cart_item_id) == False: selected_properties[cart_item_id] = [] selected_properties[cart_item_id].append({ "id": property_id, "selected_option": value }) im = IItemManagement(cart) total_items = 0 i = 1 for cart_item in im.getItems(): ci = "cart_item_%s" % i amount = self.context.REQUEST.get(ci) try: amount = int(amount) except ValueError: continue if amount < 0: continue if amount == 0: im.deleteItemByOrd(i - 1) else: cart_item.setAmount(amount) i += 1 # Set properties product = cart_item.getProduct() if IProductVariant.providedBy(product): product = product.aq_inner.aq_parent pvm = IProductVariantsManagement(product) # We need the properties also as dict to get the selected # variant. Feels somewhat dirty. TODO: Try to unify the data # model for properties. properties = {} for property in selected_properties[cart_item.getId()]: properties[property["id"]] = property["selected_option"] variant = pvm.getSelectedVariant(properties) cart_item.setProduct(variant) # TODO: At the moment we have to set the properties of the cart # item too. This is used in checkout-order-preview. Think about # to get rid of this because the properties are already available # in the variant. cart_item.setProperties(selected_properties[cart_item.getId()]) else: if selected_properties.has_key(cart_item.getId()): cart_item.setProperties( selected_properties[cart_item.getId()]) # Set selected country global and within current selected invoice # address. Why? If a customer delete all addresses the current selected # country is still saved global and can be used to calculate the # shipping price. selected_country = safe_unicode(self.request.get("selected_country")) customer.selected_country = selected_country #invoice_address = IAddressManagement(customer).getInvoiceAddress() #if invoice_address is not None: # invoice_address.country = selected_country shipping_address = IAddressManagement(customer).getShippingAddress() if shipping_address is not None: shipping_address.country = queryUtility(IIDNormalizer).normalize( selected_country) shop = IShopManagement(self.context).getShop() shipping_methods = IShippingMethodManagement(shop).getShippingMethods( check_validity=True) shipping_methods_ids = [sm.getId() for sm in shipping_methods] selected_shipping_method = self.request.get("selected_shipping_method") # Set selected shipping method if selected_shipping_method in shipping_methods_ids: customer.selected_shipping_method = \ safe_unicode(self.request.get("selected_shipping_method")) else: customer.selected_shipping_method = shipping_methods_ids[0] # Set selected payment method type customer.selected_payment_method = \ safe_unicode(self.request.get("selected_payment_method")) # Set selected VAT registration selected_vat_country = safe_unicode( self.request.get("selected_vat_country")) selected_vat_number = safe_unicode( self.request.get("selected_vat_number")) if selected_vat_country == "" or selected_vat_country is None or selected_vat_number is None: customer.vatreg = None elif selected_vat_country == "XX": customer.vatreg = selected_vat_country else: customer.vatreg = selected_vat_country + selected_vat_number
def getProducts(self): """Returns the last products, which are added to the cart. """ cm = ICurrencyManagement(self.context) result = [] for cart_item_id in self.request.SESSION.get("added-to-cart", []): cart = ICartManagement(self.context).getCart() cart_item = IItemManagement(cart).getItem(cart_item_id) if cart_item is None: continue product = cart_item.getProduct() if product is None: continue # Price price = IPrices(product).getPriceForCustomer() # Image product = cart_item.getProduct() image = IImageManagement(product).getMainImage() if image is not None: image_url = image.absolute_url() else: image_url = None # Get selected properties properties = [] pm = IPropertyManagement(product) for selected_property in cart_item.getProperties(): property_price = pm.getPriceForCustomer( selected_property["id"], selected_property["selected_option"]) # Get titles of property and option titles = getTitlesByIds(product, selected_property["id"], selected_property["selected_option"]) if titles is None: continue if (property_price == 0.0) or \ (IProductVariant.providedBy(product)) == True: show_price = False else: show_price = True properties.append({ "id": selected_property["id"], "selected_option": titles["option"], "title": titles["property"], "price": cm.priceToString(property_price), "show_price": show_price, }) price += property_price total = cart_item.getAmount() * price result.append({ "title": product.Title(), "url": product.absolute_url(), "amount": cart_item.getAmount(), "total": cm.priceToString(total), "price": cm.priceToString(price), "image_url": image_url, "properties": properties, }) # Update selected shipping method. TODO: This should be factored out and # made available via a event or similar. It is also used within # ajax/cart/_refresh_cart customer = ICustomerManagement(self.context).getAuthenticatedCustomer() shop = IShopManagement(self.context).getShop() shipping_methods = IShippingMethodManagement(shop).getShippingMethods( check_validity=True) shipping_methods_ids = [sm.getId() for sm in shipping_methods] selected_shipping_method = self.request.get("selected_shipping_method") # Set selected shipping method if selected_shipping_method in shipping_methods_ids: customer.selected_shipping_method = \ safe_unicode(self.request.get("selected_shipping_method")) else: customer.selected_shipping_method = shipping_methods_ids[0] # Reset session if self.request.SESSION.has_key("added-to-cart"): del self.request.SESSION["added-to-cart"] return result
def getProducts(self): """Returns the last products, which are added to the cart. """ cm = ICurrencyManagement(self.context) result = [] for cart_item_id in self.request.SESSION.get("added-to-cart", []): cart = ICartManagement(self.context).getCart() cart_item = IItemManagement(cart).getItem(cart_item_id) if cart_item is None: continue product = cart_item.getProduct() if product is None: continue # Price price = IPrices(product).getPriceForCustomer() # Image product = cart_item.getProduct() image = IImageManagement(product).getMainImage() if image is not None: image_url = image.absolute_url() else: image_url = None # Get selected properties properties = [] pm = IPropertyManagement(product) for selected_property in cart_item.getProperties(): property_price = pm.getPriceForCustomer( selected_property["id"], selected_property["selected_option"]) # Get titles of property and option titles = getTitlesByIds( product, selected_property["id"], selected_property["selected_option"]) if titles is None: continue if (property_price == 0.0) or \ (IProductVariant.providedBy(product)) == True: show_price = False else: show_price = True properties.append({ "id" : selected_property["id"], "selected_option" : titles["option"], "title" : titles["property"], "price" : cm.priceToString(property_price), "show_price" : show_price, }) price += property_price total = cart_item.getAmount() * price result.append({ "title" : product.Title(), "url" : product.absolute_url(), "amount" : cart_item.getAmount(), "total" : cm.priceToString(total), "price" : cm.priceToString(price), "image_url" : image_url, "properties" : properties, }) # Update selected shipping method. TODO: This should be factored out and # made available via a event or similar. It is also used within # ajax/cart/_refresh_cart customer = ICustomerManagement(self.context).getAuthenticatedCustomer() shop = IShopManagement(self.context).getShop() shipping_methods = IShippingMethodManagement(shop).getShippingMethods(check_validity=True) shipping_methods_ids = [sm.getId() for sm in shipping_methods] selected_shipping_method = self.request.get("selected_shipping_method") # Set selected shipping method if selected_shipping_method in shipping_methods_ids: customer.selected_shipping_method = \ safe_unicode(self.request.get("selected_shipping_method")) else: customer.selected_shipping_method = shipping_methods_ids[0] # Reset session if self.request.SESSION.has_key("added-to-cart"): del self.request.SESSION["added-to-cart"] return result
def _refreshCart(self): """ """ customer = ICustomerManagement(self.context).getAuthenticatedCustomer() cart = ICartManagement(self.context).getCart() if cart is None: return # Collect cart item properties for lookup selected_properties = {} for key, value in self.context.request.items(): if key.startswith("property_"): property_id, cart_item_id = key.split(":") property_id = property_id[42:] if selected_properties.has_key(cart_item_id) == False: selected_properties[cart_item_id] = [] selected_properties[cart_item_id].append({ "id" : property_id, "selected_option" : value}) im = IItemManagement(cart) total_items = 0 i = 1 for cart_item in im.getItems(): ci = "cart_item_%s" % i amount = self.context.REQUEST.get(ci) try: amount = int(amount) except ValueError: continue if amount < 0: continue if amount == 0: im.deleteItemByOrd(i-1) else: cart_item.setAmount(amount) i += 1 # Set properties product = cart_item.getProduct() if IProductVariant.providedBy(product): product = product.aq_inner.aq_parent pvm = IProductVariantsManagement(product) # We need the properties also as dict to get the selected # variant. Feels somewhat dirty. TODO: Try to unify the data # model for properties. properties = {} for property in selected_properties[cart_item.getId()]: properties[property["id"]] = property["selected_option"] variant = pvm.getSelectedVariant(properties) cart_item.setProduct(variant) # TODO: At the moment we have to set the properties of the cart # item too. This is used in checkout-order-preview. Think about # to get rid of this because the properties are already available # in the variant. cart_item.setProperties(selected_properties[cart_item.getId()]) else: if selected_properties.has_key(cart_item.getId()): cart_item.setProperties(selected_properties[cart_item.getId()]) # Set selected country global and within current selected invoice # address. Why? If a customer delete all addresses the current selected # country is still saved global and can be used to calculate the # shipping price. selected_country = safe_unicode(self.request.get("selected_country")) customer.selected_country = selected_country #invoice_address = IAddressManagement(customer).getInvoiceAddress() #if invoice_address is not None: # invoice_address.country = selected_country shipping_address = IAddressManagement(customer).getShippingAddress() if shipping_address is not None: shipping_address.country = queryUtility(IIDNormalizer).normalize(selected_country) shop = IShopManagement(self.context).getShop() shipping_methods = IShippingMethodManagement(shop).getShippingMethods(check_validity=True) shipping_methods_ids = [sm.getId() for sm in shipping_methods] selected_shipping_method = self.request.get("selected_shipping_method") # Set selected shipping method if selected_shipping_method in shipping_methods_ids: customer.selected_shipping_method = \ safe_unicode(self.request.get("selected_shipping_method")) else: customer.selected_shipping_method = shipping_methods_ids[0] # Set selected payment method type customer.selected_payment_method = \ safe_unicode(self.request.get("selected_payment_method")) # Set selected VAT registration selected_vat_country = safe_unicode(self.request.get("selected_vat_country")) selected_vat_number = safe_unicode(self.request.get("selected_vat_number")) if selected_vat_country == "" or selected_vat_country is None or selected_vat_number is None: customer.vatreg = None elif selected_vat_country == "XX": customer.vatreg = selected_vat_country else: customer.vatreg = selected_vat_country + selected_vat_number
def getCartItems(self): """Returns the items of the current cart. """ cart = self._getCart() cm = ICurrencyManagement(self.context) im = IItemManagement(cart) result = [] for cart_item in im.getItems(): product = cart_item.getProduct() product_price = IPrices(cart_item).getPriceForCustomer() / cart_item.getAmount() product_price = cm.priceToString(product_price) price = IPrices(cart_item).getPriceForCustomer() # Todo: Think about to factoring out properties stuff # because same has to be uses there: cart.py / getCartItems() properties = [] pm = IPropertyManagement(product) for selected_property in cart_item.getProperties(): property_price = pm.getPriceForCustomer( selected_property["id"], selected_property["selected_option"]) # Get titles of property and option titles = getTitlesByIds( product, selected_property["id"], selected_property["selected_option"]) if titles is None: continue if IProductVariant.providedBy(product) == True: show_price = False else: show_price = True properties.append({ "id" : selected_property["id"], "selected_option" : titles["option"], "title" : titles["property"], "price" : cm.priceToString(property_price), "show_price" : show_price, }) # Discount total_price = 0 discount = IDiscountsCalculation(cart_item).getDiscount() if discount is not None: discount_price = getMultiAdapter((discount, cart_item)).getPriceForCustomer() discount = { "title" : discount.Title(), "value" : cm.priceToString(discount_price, prefix="-"), } total_price = price - discount_price # Data data = IData(product).asDict() result.append({ "product_title" : data["title"], "product_price" : product_price, "properties" : properties, "price" : cm.priceToString(price), "amount" : cart_item.getAmount(), "total_price" : cm.priceToString(total_price), "discount" : discount, }) return result
def getCartItems(self): """Returns the items of the current cart. """ cart = self._getCart() cm = ICurrencyManagement(self.context) im = IItemManagement(cart) result = [] for cart_item in im.getItems(): product = cart_item.getProduct() product_price = IPrices( cart_item).getPriceForCustomer() / cart_item.getAmount() product_price = cm.priceToString(product_price) price = IPrices(cart_item).getPriceForCustomer() # Todo: Think about to factoring out properties stuff # because same has to be uses there: cart.py / getCartItems() properties = [] pm = IPropertyManagement(product) for selected_property in cart_item.getProperties(): property_price = pm.getPriceForCustomer( selected_property["id"], selected_property["selected_option"]) # Get titles of property and option titles = getTitlesByIds(product, selected_property["id"], selected_property["selected_option"]) if titles is None: continue if IProductVariant.providedBy(product) == True: show_price = False else: show_price = True properties.append({ "id": selected_property["id"], "selected_option": titles["option"], "title": titles["property"], "price": cm.priceToString(property_price), "show_price": show_price, }) # Discount total_price = 0 discount = IDiscountsCalculation(cart_item).getDiscount() if discount is not None: discount_price = getMultiAdapter( (discount, cart_item)).getPriceForCustomer() discount = { "title": discount.Title(), "value": cm.priceToString(discount_price, prefix="-"), } total_price = price - discount_price # Data data = IData(product).asDict() result.append({ "product_title": data["title"], "product_price": product_price, "properties": properties, "price": cm.priceToString(price), "amount": cart_item.getAmount(), "total_price": cm.priceToString(total_price), "discount": discount, }) return result