def testGetPriceForCustomer_1(self): """Test a property which is in group and product. """ pm = IPropertyManagement(self.shop.products.product_1) price = pm.getPriceForCustomer("color", "Red") self.assertEqual("%.2f" % price, "-9.24") price = pm.getPriceForCustomer("color", "Blue") self.assertEqual(price, 0.0) price = pm.getPriceForCustomer("color", "Green") self.assertEqual("%.2f" % price, "13.87")
def testGetPriceForCustomer_2(self): """Test a property which is just in group. """ pm = IPropertyManagement(self.shop.products.product_1) price = pm.getPriceForCustomer("size", "Small") self.assertEqual("%.2f" % price, "-10.17") price = pm.getPriceForCustomer("size", "Medium") self.assertEqual("%.2f" % price, "0.92") price = pm.getPriceForCustomer("size", "Large") self.assertEqual("%.2f" % price, "20.34")
def getPriceForCustomer(self, with_discount=False): """Returns the customer price for a cart item. This is just the customer product price plus the customer properties prices (can be positiv or negative) multiply with the amount. """ product = self.context.getProduct() price = IPrices(product).getPriceForCustomer() # Add prices of selected properties pm = IPropertyManagement(product) for selected_property in self.context.getProperties(): price += pm.getPriceForCustomer( selected_property["id"], selected_property["selected_option"] ) price *= self.context.getAmount() if with_discount == True: discount = IDiscountsCalculation(self.context).getDiscount() if discount is not None: discount_value = getMultiAdapter( (discount, self.context)).getPriceForCustomer() price -= discount_value return price
def getPriceForCustomer(self, formatted=True): """ """ p = IPrices(self.context) price = p.getPriceForCustomer() if IProductVariantsManagement(self.context).hasVariants() == False: total_diff = 0.0 pm = IPropertyManagement(self.context) for property_id, selected_option in self.request.form.items(): if property_id.startswith("property"): total_diff += pm.getPriceForCustomer( property_id[42:], selected_option ) price += total_diff if formatted == True: cm = ICurrencyManagement(self.context) return cm.priceToString(price, suffix=None) else: return price
def getStandardPriceForCustomer(self, formatted=True): """Returns the standard price for a customer when the product is for sale. Used to display the crossed-out standard price. """ p = IPrices(self.context) price = p.getPriceForCustomer(effective=False) if IProductVariantsManagement(self.context).hasVariants() == False: total_diff = 0.0 pm = IPropertyManagement(self.context) for property_id, selected_option in self.request.form.items(): if property_id.startswith("property"): total_diff += pm.getPriceForCustomer( property_id[42:], selected_option ) price + total_diff if formatted == True: cm = ICurrencyManagement(self.context) return cm.priceToString(price, suffix = None) else: return price
def getPriceForCustomer(self, with_discount=False): """Returns the customer price for a cart item. This is just the customer product price plus the customer properties prices (can be positiv or negative) multiply with the amount. """ product = self.context.getProduct() price = IPrices(product).getPriceForCustomer() # Add prices of selected properties pm = IPropertyManagement(product) for selected_property in self.context.getProperties(): price += pm.getPriceForCustomer( selected_property["id"], selected_property["selected_option"]) price *= self.context.getAmount() if with_discount == True: discount = IDiscountsCalculation(self.context).getDiscount() if discount is not None: discount_value = getMultiAdapter( (discount, self.context)).getPriceForCustomer() price -= discount_value return price
def _addItemFromCartItem(self, id, cart_item): """Sets the item by given cart item. """ self.context.manage_addProduct["easyshop.core"].addOrderItem(id=str(id)) new_item = getattr(self.context, str(id)) # set product quantity new_item.setProductQuantity(cart_item.getAmount()) # Set product prices & taxes product_taxes = ITaxes(cart_item.getProduct()) product_prices = IPrices(cart_item.getProduct()) item_prices = IPrices(cart_item) item_taxes = ITaxes(cart_item) new_item.setTaxRate(product_taxes.getTaxRateForCustomer()) new_item.setProductTax(product_taxes.getTaxForCustomer()) new_item.setProductPriceGross(product_prices.getPriceForCustomer()) new_item.setProductPriceNet(product_prices.getPriceNet()) # Set item prices & taxes new_item.setTax(item_taxes.getTaxForCustomer()) new_item.setPriceGross(item_prices.getPriceForCustomer()) new_item.setPriceNet(item_prices.getPriceNet()) # Discount discount = IDiscountsCalculation(cart_item).getDiscount() if discount is not None: new_item.setDiscountDescription(discount.Title()) dp = getMultiAdapter((discount, cart_item)) new_item.setDiscountGross(dp.getPriceForCustomer()) new_item.setDiscountNet(dp.getPriceNet()) # Set product product = cart_item.getProduct() new_item.setProduct(product) # Set product name and id data = IData(product).asDict() new_item.setProductTitle(data["title"]) new_item.setArticleId(data["article_id"]) # Set properties properties = [] pm = IPropertyManagement(product) for selected_property in cart_item.getProperties(): # Get the price property_price = pm.getPriceForCustomer( selected_property["id"], selected_property["selected_option"]) # By default we save the titles of the properties and selected # options In this way they are kept if the title of a property or # option will be changed after the product has been bought. titles = getTitlesByIds( product, selected_property["id"], selected_property["selected_option"]) # If we don't find the property or option we ignore the property. # This can only happen if the property has been deleted after a # product has been added to the cart. In this case we don't want the # property at all (I think). if titles is None: continue properties.append({ "title" : titles["property"], "selected_option" : titles["option"], "price" : str(property_price), }) new_item.setProperties(properties)
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 _addItemFromCartItem(self, id, cart_item): """Sets the item by given cart item. """ self.context.manage_addProduct["easyshop.core"].addOrderItem( id=str(id)) new_item = getattr(self.context, str(id)) # set product quantity new_item.setProductQuantity(cart_item.getAmount()) # Set product prices & taxes product_taxes = ITaxes(cart_item.getProduct()) product_prices = IPrices(cart_item.getProduct()) item_prices = IPrices(cart_item) item_taxes = ITaxes(cart_item) new_item.setTaxRate(product_taxes.getTaxRateForCustomer()) new_item.setProductTax(product_taxes.getTaxForCustomer()) new_item.setProductPriceGross(product_prices.getPriceForCustomer()) new_item.setProductPriceNet(product_prices.getPriceNet()) # Set item prices & taxes new_item.setTax(item_taxes.getTaxForCustomer()) new_item.setPriceGross(item_prices.getPriceForCustomer()) new_item.setPriceNet(item_prices.getPriceNet()) # Discount discount = IDiscountsCalculation(cart_item).getDiscount() if discount is not None: new_item.setDiscountDescription(discount.Title()) dp = getMultiAdapter((discount, cart_item)) new_item.setDiscountGross(dp.getPriceForCustomer()) new_item.setDiscountNet(dp.getPriceNet()) # Set product product = cart_item.getProduct() new_item.setProduct(product) # Set product name and id data = IData(product).asDict() new_item.setProductTitle(data["title"]) new_item.setArticleId(data["article_id"]) # Set properties properties = [] pm = IPropertyManagement(product) for selected_property in cart_item.getProperties(): # Get the price property_price = pm.getPriceForCustomer( selected_property["id"], selected_property["selected_option"]) # By default we save the titles of the properties and selected # options In this way they are kept if the title of a property or # option will be changed after the product has been bought. titles = getTitlesByIds(product, selected_property["id"], selected_property["selected_option"]) # If we don't find the property or option we ignore the property. # This can only happen if the property has been deleted after a # product has been added to the cart. In this case we don't want the # property at all (I think). if titles is None: continue properties.append({ "title": titles["property"], "selected_option": titles["option"], "price": str(property_price), }) new_item.setProperties(properties)
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