def addOrder(self, customer=None, cart=None): """ """ cartmanager = ICartManagement(self.context) if customer is None: cm = ICustomerManagement(self.context) customer = cm.getAuthenticatedCustomer() if cart is None: cart = cartmanager.getCart() portal = getToolByName(self.context, 'portal_url').getPortalObject() ## The current user may not be allowed to create an order, so we ## temporarily change the security context to use a temporary ## user with manager role. old_sm = getSecurityManager() tmp_user = UnrestrictedUser(old_sm.getUser().getId(), '', ['Manager'], '') tmp_user = tmp_user.__of__(portal.acl_users) newSecurityManager(None, tmp_user) # Add a new order new_id = self._createOrderId() self.orders.invokeFactory("Order", id=new_id) new_order = getattr(self.orders, new_id) # Copy Customer to Order customer = ICustomerManagement(self.context).getAuthenticatedCustomer() cm = ICopyManagement(customer) cm.copyTo(new_order) # Add cart items to order IItemManagement(new_order).addItemsFromCart(cart) # Add total tax new_order.setTax(ITaxes(cart).getTaxForCustomer()) # Add shipping values to order sm = IShippingPriceManagement(self.context) new_order.setShippingPriceNet(sm.getPriceNet()) new_order.setShippingPriceGross(sm.getPriceForCustomer()) new_order.setShippingTax(sm.getTaxForCustomer()) new_order.setShippingTaxRate(sm.getTaxRateForCustomer()) # Add payment price values to order pp = IPaymentPriceManagement(self.context) new_order.setPaymentPriceGross(pp.getPriceForCustomer()) new_order.setPaymentPriceNet(pp.getPriceNet()) new_order.setPaymentTax(pp.getTaxForCustomer()) new_order.setPaymentTaxRate(pp.getTaxRateForCustomer()) ## Reset security manager setSecurityManager(old_sm) # Index with customer again new_order.reindexObject() return new_order
def addItemsFromCart(self, cart): """Adds all items from a given cart to the order """ shop = IShopManagement(self.context).getShop() if cart is None: cartmanager = ICartManagement(shop) cart = cartmanager.getCart() # edit cart items id = 0 for cart_item in IItemManagement(cart).getItems(): id += 1 self._addItemFromCartItem(id, cart_item)
def getPriceForCustomer(self): """ """ # If there a no items the shipping price for cutomers is zero. cart_manager = ICartManagement(self.context) cart = cart_manager.getCart() if cart is None: return 0 cart_item_manager = IItemManagement(cart) if cart_item_manager.hasItems() == False: return 0 return self.getPriceNet() + self.getTaxForCustomer()
def getPriceForCustomer(self): """ """ # If there a no items the payment price for cutomers is zero. cart_manager = ICartManagement(self.context) cart = cart_manager.getCart() if cart is None: return 0 cart_item_manager = IItemManagement(cart) if cart_item_manager.hasItems() == False: return 0 return self.getPriceNet() + self.getTaxForCustomer()
def getTaxForCustomer(self): """ """ # If there a no items the shipping tax is 0 cart_manager = ICartManagement(self.context) cart = cart_manager.getCart() if cart is None: return 0 cart_item_manager = IItemManagement(cart) if cart_item_manager.hasItems() == False: return 0 temp_shipping_product = self._createTemporaryShippingProduct() return ITaxes(temp_shipping_product).getTaxForCustomer()
def testAddItemsFromCart(self): """ """ view = getMultiAdapter((self.shop.products.product_1, self.shop.products.product_1.REQUEST), name="addToCart") view.addToCart() view = getMultiAdapter((self.shop.products.product_2, self.shop.products.product_2.REQUEST), name="addToCart") view.addToCart() cm = ICartManagement(self.shop) cart = cm.getCart() im = IItemManagement(self.order) im.addItemsFromCart(cart) product_ids = [item.getProduct().getId() for item in im.getItems()] self.assertEqual(product_ids, ["product_1", "product_2"])
def addOrder(self, customer=None, cart=None): """ """ cartmanager = ICartManagement(self.context) if customer is None: cm = ICustomerManagement(self.context) customer = cm.getAuthenticatedCustomer() if cart is None: cart = cartmanager.getCart() portal = getToolByName(self.context, 'portal_url').getPortalObject() ## The current user may not be allowed to create an order, so we ## temporarily change the security context to use a temporary ## user with manager role. old_sm = getSecurityManager() tmp_user = UnrestrictedUser( old_sm.getUser().getId(), '', ['Manager'], '' ) tmp_user = tmp_user.__of__(portal.acl_users) newSecurityManager(None, tmp_user) # Add a new order new_id = self._createOrderId() self.orders.invokeFactory("Order", id=new_id) new_order = getattr(self.orders, new_id) # Copy Customer to Order customer = ICustomerManagement(self.context).getAuthenticatedCustomer() cm = ICopyManagement(customer) cm.copyTo(new_order) # Add cart items to order IItemManagement(new_order).addItemsFromCart(cart) # Add total tax new_order.setTax(ITaxes(cart).getTaxForCustomer()) # Add shipping values to order sm = IShippingPriceManagement(self.context) new_order.setShippingPriceNet(sm.getPriceNet()) new_order.setShippingPriceGross(sm.getPriceForCustomer()) new_order.setShippingTax(sm.getTaxForCustomer()) new_order.setShippingTaxRate(sm.getTaxRateForCustomer()) # Add payment price values to order pp = IPaymentPriceManagement(self.context) new_order.setPaymentPriceGross(pp.getPriceForCustomer()) new_order.setPaymentPriceNet(pp.getPriceNet()) new_order.setPaymentTax(pp.getTaxForCustomer()) new_order.setPaymentTaxRate(pp.getTaxRateForCustomer()) ## Reset security manager setSecurityManager(old_sm) # Index with customer again new_order.reindexObject() return new_order
def handle_buy_action(self, action, data): """Buys a cart. """ putils = getToolByName(self.context, "plone_utils") # add order om = IOrderManagement(self.context) new_order = om.addOrder() # Set message to shop owner new_order.setMessage(self.context.request.get("form.message", "")) # process payment result = IPaymentProcessing(new_order).process() # Need error for payment methods for which the customer has to pay at # any case The order process should not go on if the customer is not # able to pay. if result.code == ERROR: om.deleteOrder(new_order.id) putils.addPortalMessage(result.message, type=u"error") ICheckoutManagement(self.context).redirectToNextURL("ERROR_PAYMENT") return "" else: cm = ICartManagement(self.context) # Decrease stock IStockManagement(self.context).removeCart(cm.getCart()) # Delete cart cm.deleteCart() # Set order to pending (Mails will be sent) wftool = getToolByName(self.context, "portal_workflow") wftool.doActionFor(new_order, "submit") putils.addPortalMessage(MESSAGES["ORDER_RECEIVED"]) if result.code == PAYED: # Set order to payed (Mails will be sent) wftool = getToolByName(self.context, "portal_workflow") # We need a new security manager here, because this transaction # should usually just be allowed by a Manager except here. old_sm = getSecurityManager() tmp_user = UnrestrictedUser( old_sm.getUser().getId(), '', ['Manager'], '' ) portal = getToolByName(self.context, 'portal_url').getPortalObject() tmp_user = tmp_user.__of__(portal.acl_users) newSecurityManager(None, tmp_user) wftool.doActionFor(new_order, "pay_not_sent") ## Reset security manager setSecurityManager(old_sm) # Redirect customer = \ ICustomerManagement(self.context).getAuthenticatedCustomer() selected_payment_method = \ IPaymentInformationManagement(customer).getSelectedPaymentMethod() if not IAsynchronPaymentMethod.providedBy(selected_payment_method): ICheckoutManagement(self.context).redirectToNextURL("BUYED_ORDER")
class TestCartManagement(EasyShopTestCase): """ """ def afterSetUp(self): """ """ super(TestCartManagement, self).afterSetUp() self.cm = ICartManagement(self.shop) def testCreateCart_1(self): """Create cart for anonymous. """ self.logout() cart = self.cm.createCart() self.assertEqual(cart.getId(), "123") def testCreateCart_2(self): """Create cart for member. """ self.login("newmember") cart = self.cm.createCart() self.assertEqual(cart.getId(), "newmember") def testDeleteCart_1(self): """Without given id. """ self.login("newmember") self.cm.createCart() self.failUnless(self.shop.carts.get("newmember")) self.cm.deleteCart() self.failIf(self.shop.carts.get("newmember")) def testDeleteCart_2(self): """With given id. """ self.login("newmember") self.cm.createCart() self.failUnless(self.shop.carts.get("newmember")) self.cm.deleteCart("newmember") self.failIf(self.shop.carts.get("newmember")) def testGetCart_1(self): """Cart for anonmyous. There is no cart yet. """ self.logout() cart = self.cm.getCart() self.assertEqual(cart, None) def testGetCart_2(self): """Cart for anonymous. There is a cart """ self.logout() self.cm.createCart() cart = self.cm.getCart() self.assertEqual(cart.getId(), "123") def testGetCart_3(self): """Cart for member. There is no anonymous cart. Carts are only created when items are added to it. """ self.login("newmember") cart = self.cm.getCart() self.assertEqual(cart, None) def testGetCart_4(self): """Cart for member. There is an anonymous cart. """ self.logout() # create cart for anonymous self.cm.createCart() cart = self.cm.getCart() self.assertEqual(cart.getId(), "123") self.login("newmember") # After login the anonymous cart should be moved to member cart cart = self.cm.getCart() self.assertEqual(cart.getId(), "newmember") # The cart for anonymous has to be deleted self.failIf(self.shop.carts.get("123")) def testGetCarts(self): """ """ # create cart for newmember self.login("newmember") self.cm.createCart() self.setRoles(("Manager",)) ids = [c.getId for c in self.cm.getCarts()] self.assertEqual(ids, ["newmember"]) def testGetCartById(self): """ """ # create cart for newmember self.login("newmember") self.cm.createCart() cart = self.cm.getCartById("newmember") self.assertEqual(cart.getId(), "newmember") def testGetCartByUID(self): """ """ # create cart for newmember self.login("newmember") cart = self.cm.createCart() cart = self.cm.getCartByUID(cart.UID()) self.assertEqual(cart.getId(), "newmember") def testHasCart(self): """ """ self.assertEqual(self.cm.hasCart(), False) self.login("newmember") cart = self.cm.createCart() self.assertEqual(self.cm.hasCart(), True) def test_getCartId(self): """ """ self.logout() cart_id = self.cm._getCartId() self.assertEqual(cart_id, "123") self.login("newmember") cart_id = self.cm._getCartId() self.assertEqual(cart_id, "newmember")
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 handle_buy_action(self, action, data): """Buys a cart. """ putils = getToolByName(self.context, "plone_utils") # add order om = IOrderManagement(self.context) new_order = om.addOrder() # Set message to shop owner new_order.setMessage(self.context.request.get("form.message", "")) # process payment result = IPaymentProcessing(new_order).process() # Need error for payment methods for which the customer has to pay at # any case The order process should not go on if the customer is not # able to pay. if result.code == ERROR: om.deleteOrder(new_order.id) putils.addPortalMessage(result.message, type=u"error") ICheckoutManagement( self.context).redirectToNextURL("ERROR_PAYMENT") return "" else: cm = ICartManagement(self.context) # Decrease stock IStockManagement(self.context).removeCart(cm.getCart()) # Delete cart cm.deleteCart() # Set order to pending (Mails will be sent) wftool = getToolByName(self.context, "portal_workflow") wftool.doActionFor(new_order, "submit") putils.addPortalMessage(MESSAGES["ORDER_RECEIVED"]) if result.code == PAYED: # Set order to payed (Mails will be sent) wftool = getToolByName(self.context, "portal_workflow") # We need a new security manager here, because this transaction # should usually just be allowed by a Manager except here. old_sm = getSecurityManager() tmp_user = UnrestrictedUser(old_sm.getUser().getId(), '', ['Manager'], '') portal = getToolByName(self.context, 'portal_url').getPortalObject() tmp_user = tmp_user.__of__(portal.acl_users) newSecurityManager(None, tmp_user) wftool.doActionFor(new_order, "pay_not_sent") ## Reset security manager setSecurityManager(old_sm) # Redirect customer = \ ICustomerManagement(self.context).getAuthenticatedCustomer() selected_payment_method = \ IPaymentInformationManagement(customer).getSelectedPaymentMethod() if not IAsynchronPaymentMethod.providedBy(selected_payment_method): ICheckoutManagement(self.context).redirectToNextURL("BUYED_ORDER")