def getStockInformation(self): """ """ shop = self._getShop() sm = IStockManagement(shop) pvm = IProductVariantsManagement(self.context) if pvm.hasVariants() == False: stock_information = sm.getStockInformationFor(self.context) else: product_variant = pvm.getSelectedVariant() # First, we try to get information for the selected product variant stock_information = sm.getStockInformationFor(product_variant) # If nothing is found, we try to get information for parent product # variants object. if stock_information is None: stock_information = sm.getStockInformationFor(self.context) if stock_information is None: return None return IData(stock_information).asDict()
def testAsDict4(self): """ """ self.si.setDeliveryTimeMin("1") self.si.setDeliveryTimeMax("1") self.si.setDeliveryTimeUnit("Weeks") data = IData(self.si).asDict() self.assertEqual(data["time_period"], "1") self.assertEqual(data["time_unit"], "Week")
def testAsDict2(self): """ """ self.si.setDeliveryTimeMin("2") self.si.setDeliveryTimeMax("2") self.si.setDeliveryTimeUnit("Days") data = IData(self.si).asDict() self.assertEqual(data["time_period"], "2") self.assertEqual(data["time_unit"], "Days")
def getCartItems(self): """ """ shop = IShopManagement(self.context).getShop() cart = self._getCart() # If there isn't a cart yet if cart is None: return [] cm = ICurrencyManagement(self.context) result = [] for cart_item in IItemManagement(cart).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() # 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 # Product title data = IData(product).asDict() title = data["title"] result.append({ "id": cart_item.getId(), "product_title": title, "product_url": product.absolute_url(), "product_price": product_price, "price": cm.priceToString(price), "amount": cart_item.getAmount(), "properties": self._getProperties(cart_item), "total_price": cm.priceToString(total_price), "discount": discount, }) return result
def asDict(self): """ """ pvm = IProductVariantsManagement(self.context) if pvm.hasVariants() == True: variant = pvm.getSelectedVariant() or pvm.getDefaultVariant() return IData(variant).asDict() else: # price cm = ICurrencyManagement(self.context) price = IPrices(self.context).getPriceForCustomer() price = cm.priceToString(price) # image image = IImageManagement(self.context).getMainImage() if image is not None: image = "%s/image_%s" % (image.absolute_url(), "preview") images = [] for temp in IImageManagement(self.context).getImages(): images.append("%s/image_tile" % temp.absolute_url()) return { "article_id": self.context.getArticleId(), "title": self.context.Title(), "short_title": self.context.getShortTitle() or self.context.Title(), "description": self.context.Description(), "url": self.context.absolute_url(), "price": price, "image": image, "images": images, "text": self.context.getText(), "short_text": self.context.getShortText(), }
def getStockInformations(self): """ """ shop = IShopManagement(self.context).getShop() sm = IStockManagement(shop) result = [] for stock_information in sm.getStockInformations(): data = IData(stock_information).asDict() result.append({ "id" : stock_information.getId(), "title" : stock_information.Title(), "description" : stock_information.Description(), "available" : data["available"], "time_period" : data["time_period"], "url" : stock_information.absolute_url(), "up_url" : "%s/es_folder_position?position=up&id=%s" % (self.context.absolute_url(), stock_information.getId()), "down_url" : "%s/es_folder_position?position=down&id=%s" % (self.context.absolute_url(), stock_information.getId()), "amount_of_criteria" : len(stock_information.objectIds()), }) 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 getProductData(self): """ """ data = IData(self.context) return data.asDict()
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 getStockInformation(self): """ """ return IData(self.context).asDict()