def getVariants(self): """ """ result = [] pvm = IProductVariantsManagement(self.context) for variant in pvm.getVariants(): # Options properties = [] properties_ids = {} for property in variant.getForProperties(): property_id, option_id = property.split(":") properties_ids[property_id] = option_id titles = getTitlesByIds(variant, property_id, option_id) if titles is None: continue properties.append(titles) result.append({ "id": variant.getId(), "path": "/".join(variant.getPhysicalPath()), "article_id": variant.getArticleId(), "title": variant.Title(), "url": variant.absolute_url(), "properties": properties, "properties_ids": properties_ids, "price": variant.getPrice(), }) return result
def getVariants(self): """ """ result = [] pvm = IProductVariantsManagement(self.context) for variant in pvm.getVariants(): # Options properties = [] properties_ids = {} for property in variant.getForProperties(): property_id, option_id = property.split(":") properties_ids[property_id] = option_id titles = getTitlesByIds(variant, property_id, option_id) if titles is None: continue properties.append(titles) result.append({ "id" : variant.getId(), "path" : "/".join(variant.getPhysicalPath()), "article_id" : variant.getArticleId(), "title" : variant.Title(), "url" : variant.absolute_url(), "properties" : properties, "properties_ids" : properties_ids, "price" : variant.getPrice(), }) 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 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