def updateAccessories(self): """ """ uids = self.request.get("accessories", []) uids = utils.tuplize(uids) uids_with_quantities = [] for new_uid in uids: quantity = self.request.get("%s_quantity" % new_uid, 1) uids_with_quantities.append("%s:%s" % (new_uid, quantity)) self.context.setAccessories(uids_with_quantities) self.redirect()
def getTotalStandardPriceForCustomer(self): """ """ selected_accessories = self.request.get("accessories", []) selected_accessories = tuplize(selected_accessories) product_price = self.getStandardPriceForCustomer(formatted=False) total_accessories = 0 for accessory in self.getAccessories(): # We take only selected accessories into account if accessory["uid"] in selected_accessories: total_accessories += accessory["total_raw_standard_price"] total_price = product_price + total_accessories cm = ICurrencyManagement(self.context) return cm.priceToString(total_price, suffix=None)
def addAccessories(self): """ """ # Collect new products new_uids = self.request.form.get("new-products", []) new_uids = utils.tuplize(new_uids) # Add quantities to new products new_uids_with_quantities = [] for new_uid in new_uids: quantity = self.request.get("%s_quantity" % new_uid, 1) new_uids_with_quantities.append("%s:%s" % (new_uid, quantity)) existing_uids = self.context.getAccessories() unique_uids = self.unify(existing_uids, new_uids_with_quantities) self.context.setAccessories(unique_uids) self.redirect()
def addToCart(self, redirect=True, add_accessories=True): """ """ shop = IShopManagement(self.context).getShop() cm = ICartManagement(shop) cart = cm.getCart() if cart is None: cart = cm.createCart() pvm = IProductVariantsManagement(self.context) if pvm.hasVariants(): # Get the actual "product" product = pvm.getSelectedVariant() or pvm.getDefaultVariant() # Using here the selected product ensures that we save the right # properties. This is important when the selected variant doesn't # exist. properties = [] for property in product.getForProperties(): property_id, selected_option = property.split(":") properties.append({ "id": property_id, "selected_option": selected_option, }) else: # The product is the context product = self.context # Unlike above we take the properties out of the request, because # there is no object wich stores the different properties. properties = [] for property in IPropertyManagement(product).getProperties(): selected_option_id = self.request.get( "property_%s_%s" % (product.UID(), property.getId())) # If nothing is selected we take the first option of the # property if (selected_option_id is None) or (selected_option_id == "select"): property = IPropertyManagement(product).getProperty( property.getId()) property_options = property.getOptions() if property_options: selected_option = property.getOptions()[0] selected_option_id = selected_option["id"] else: selected_option_id = "" properties.append({ "id": property.getId(), "selected_option": selected_option_id }) # get quantity quantity = int( self.context.request.get("%s_quantity" % self.context.UID(), 1)) # returns true if the product was already within the cart result, item_id = IItemManagement(cart).addItem( product, tuple(properties), quantity) # Add product to session (for display on add to cart view) if self.request.SESSION.get("added-to-cart") is None: self.request.SESSION["added-to-cart"] = [] self.request.SESSION["added-to-cart"].append(item_id) # Add the accessories if add_accessories == True: catalog = getToolByName(self.context, "portal_catalog") accessories = tuplize(self.request.get("accessories", [])) for uid in accessories: try: brain = catalog.searchResults(UID=uid)[0] except IndexError: continue # We reuse the same view with an other context. The context are # the accessories product = brain.getObject() view = getMultiAdapter((product, self.request), name="addToCart") view.addToCart(redirect=False, add_accessories=False) if redirect == True: # Set portal message # putils = getToolByName(self.context, "plone_utils") # if result == True: # putils.addPortalMessage(MESSAGES["CART_INCREASED_AMOUNT"]) # else: # putils.addPortalMessage(MESSAGES["CART_ADDED_PRODUCT"]) url = "%s/added-to-cart" % shop.absolute_url() self.context.request.response.redirect(url)
def getAccessories(self): """ """ catalog = getToolByName(self.context, "portal_catalog") accessories = tuplize(self.request.get("accessories", [])) result = [] for uid_with_quantiy in self.context.getAccessories(): uid, quantity = uid_with_quantiy.split(":") try: brain = catalog.searchResults(UID=uid)[0] except IndexError: # This could happend if the user doesn't have the View # permission (e.g. product is private) continue product = brain.getObject() # Try to get quantity out of request (In case the customer has # changed them within the form). If there is none we take the default # quantity which are given from the shop owner within the accessories # admin interface. quantity = self.request.get("%s_quantity" % product.UID(), quantity) # Same viewlet with the context of the accessory to get the # properties of the accessory. viewlet = getMultiAdapter((product, self.request, self.view, self.manager), IViewlet, name="easyshop.product-viewlet") properties = viewlet.getProperties() try: quantity = int(quantity) except ValueError: quantity = 1 # Standard price standard_price = viewlet.getStandardPriceForCustomer(formatted=False) total_raw_standard_price = quantity * standard_price cm = ICurrencyManagement(self.context) standard_price = cm.priceToString(standard_price, suffix=None) total_standard_price = cm.priceToString(total_raw_standard_price, suffix=None) # Effective price price = viewlet.getPriceForCustomer(formatted=False) total_raw_price = quantity * price cm = ICurrencyManagement(self.context) price = cm.priceToString(price, suffix=None) total_price = cm.priceToString(total_raw_price, suffix=None) result.append({ "uid" : uid, "title" : brain.Title, "quantity" : quantity, "checked" : uid in accessories, "for_sale" : product.getForSale(), "total_raw_price" : total_raw_price, "total_raw_standard_price" : total_raw_standard_price, "standard_price" : standard_price, "total_standard_price" : total_standard_price, "price" : price, "total_price" : total_price, "properties" : properties, }) return result