def testAddProductAsAnonymousAndMember(self): """Proves that a member will overtake the cart items, which he has added, as anonymous user. """ self.logout() view = getMultiAdapter((self.shop.products.product_2, self.shop.products.product_2.REQUEST), name="addToCart") view.addToCart() cart = ICartManagement(self.shop).getCart() items = IItemManagement(cart).getItems() self.assertEqual(len(items), 1) view.addToCart() items = IItemManagement(cart).getItems() self.assertEqual(len(items), 1) view = getMultiAdapter((self.shop.products.product_1, self.shop.products.product_1.REQUEST), name="addToCart") view.addToCart() items = IItemManagement(cart).getItems() self.assertEqual(len(items), 2) self.login("newmember") cart = ICartManagement(self.shop).getCart() items = IItemManagement(cart).getItems() self.assertEqual(len(items), 2) self.assertEqual(cart.getId(), "newmember")
def testAddItemFromCartItem(self): """ """ cm = ICartManagement(self.shop) cart = cm.createCart() properties = ( { "id": "color", "selected_option": "Red" }, # -10.0 { "id": "material", "selected_option": "Wood" }, # 0.0 { "id": "quality", "selected_option": "High" }, # 1500.0 ) cim = IItemManagement(cart) cim.addItem(self.product_1, properties=properties, quantity=3) cart_item = IItemManagement(cart).getItems()[0] oim = IItemManagement(self.order) oim._addItemFromCartItem("0", cart_item) order_item = oim.getItems()[0] self.assertEqual(order_item.getProductQuantity(), 3) self.assertEqual("%.2f" % order_item.getProductPriceGross(), "22.00") self.assertEqual("%.2f" % order_item.getProductPriceNet(), "18.49") self.assertEqual("%.2f" % order_item.getProductTax(), "3.51") self.assertEqual("%.2f" % order_item.getPriceGross(), "4536.00") self.assertEqual("%.2f" % order_item.getPriceNet(), "3811.76") self.assertEqual(order_item.getTaxRate(), 19.0) self.assertEqual("%.2f" % order_item.getTax(), "724.24") self.assertEqual(order_item.getProduct(), self.product_1) properties = order_item.getProperties() self.assertEqual(properties[0]["title"], "Color") self.assertEqual(properties[0]["selected_option"], "Red") self.assertEqual(properties[0]["price"], "-10.0") self.assertEqual(properties[1]["title"], "Material") self.assertEqual(properties[1]["selected_option"], "Wood") self.assertEqual(properties[1]["price"], "0.0") self.assertEqual(properties[2]["title"], "Quality") self.assertEqual(properties[2]["selected_option"], "High") self.assertEqual(properties[2]["price"], "1500.0")
def isValid(self, product=None): """Returns True, if the total width of the cart is greater than the entered criteria width. """ shop = IShopManagement(self.context).getShop() cart = ICartManagement(shop).getCart() max_length = 0 max_width = 0 total_height = 0 if cart is not None: for item in IItemManagement(cart).getItems(): if max_length < item.getProduct().getLength(): max_length = item.getProduct().getLength() if max_width < item.getProduct().getWidth(): max_width = item.getProduct().getWidth() total_height += (item.getProduct().getHeight() * item.getAmount()) # Calc cart girth cart_girth = (2 * max_width) + (2 * total_height) + max_length if self.context.getOperator() == ">=": if cart_girth >= self.context.getCombinedLengthAndGirth(): return True else: if cart_girth < self.context.getCombinedLengthAndGirth(): return True return False
def testDeleteItem(self): """ """ self.logout() cm = ICartManagement(self.shop) cart = cm.createCart() im = IItemManagement(cart) im.addItem(self.product_1, properties=()) self.assertEqual(len(im.getItems()), 1) im.addItem(self.product_2, properties=(), quantity=3) self.assertEqual(len(im.getItems()), 2) # Try to delete non existing id result = im.deleteItem("3") self.assertEqual(result, False) # Still 2 items in there self.assertEqual(len(im.getItems()), 2) # delete first item result = im.deleteItem("0") self.assertEqual(result, True) self.assertEqual(len(im.getItems()), 1) # delete second item result = im.deleteItem("1") self.assertEqual(result, True) self.assertEqual(len(im.getItems()), 0)
def getDiscounts(self): """ """ return [] cm = ICurrencyManagement(self.context) cart = self._getCart() if cart is None: return [] discounts = [] for cart_item in IItemManagement(cart).getItems(): discount = IDiscountsCalculation(cart_item).getDiscount() if discount is not None: value = getMultiAdapter( (discount, cart_item)).getPriceForCustomer() discounts.append({ "title": discount.Title(), "value": cm.priceToString(value, prefix="-", suffix=None), }) return discounts
def afterSetUp(self): """ """ super(TestCartItemProperties, self).afterSetUp() self.login("newmember") cm = ICartManagement(self.shop) cart = cm.createCart() im = IItemManagement(cart) properties = ( { "id": "color", "selected_option": "Red" }, # -10.0 { "id": "material", "selected_option": "Wood" }, # 0.0 { "id": "quality", "selected_option": "High" }, # 1500.0 ) im.addItem(self.product_1, properties=properties, quantity=3) self.item1 = im.getItems()[0]
def testAddItemsAndGetItems(self): """ """ self.logout() cm = ICartManagement(self.shop) cart = cm.createCart() im = IItemManagement(cart) im.addItem(self.product_1, properties=()) self.assertEqual(len(im.getItems()), 1) im.addItem(self.product_1, properties=()) self.assertEqual(len(im.getItems()), 1) im.addItem(self.product_1, properties=(), quantity=2) self.assertEqual(len(im.getItems()), 1) im.addItem(self.product_2, properties=(), quantity=3) self.assertEqual(len(im.getItems()), 2) i1, i2 = im.getItems() i1.getId() == "0" i1.getProduct() == self.product_1 i1.getAmount() == 4 i1.getId() == "1" i1.getProduct() == self.product_2 i1.getAmount() == 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 getPriceNet(self, with_shipping=True, with_payment=True, with_discount=True): """Returns the net price of the cart. This is just a sum over net prices of all items of the cart plus shipping and payment. """ im = IItemManagement(self.context) if im.hasItems() == False: return 0.0 price = 0.0 for cart_item in im.getItems(): # NOTE: with_discount is passed here price += IPrices(cart_item).getPriceNet( with_discount=with_discount) if with_shipping == True: sm = IShippingPriceManagement(self.shop) shipping_price = sm.getPriceNet() price += shipping_price if with_payment == True: sm = IPaymentPriceManagement(self.shop) payment_price = sm.getPriceNet() price += payment_price return price
def testPrices3(self): """ """ self.shop.discounts.invokeFactory("Discount", id="d1", title="D1", value="1.0", base="cart_item", type="percentage") discount = self.shop.discounts.d1 view = getMultiAdapter((self.shop.products.product_1, self.shop.products.product_1.REQUEST), name="addToCart") view() view() cart = ICartManagement(self.shop).getCart() item = IItemManagement(cart).getItems()[0] prices = getMultiAdapter((discount, item), IPrices) price_net = "%.2f" % prices.getPriceNet() price_gross = "%.2f" % prices.getPriceGross() price_for_customer = "%.2f" % prices.getPriceForCustomer() self.assertEqual(price_gross, "0.44") self.assertEqual(price_for_customer, "0.44") self.assertEqual(price_net, "0.37")
def testDeleteItemByOrd(self): """ """ self.logout() cm = ICartManagement(self.shop) cart = cm.createCart() im = IItemManagement(cart) im.addItem(self.product_1, properties=()) self.assertEqual(len(im.getItems()), 1) im.addItem(self.product_2, properties=(), quantity=3) self.assertEqual(len(im.getItems()), 2) # Try to delete non existing ord result = im.deleteItemByOrd(3) self.assertEqual(result, False) # Still 2 items in there self.assertEqual(len(im.getItems()), 2) # delete first item result = im.deleteItemByOrd(0) self.assertEqual(result, True) self.assertEqual(len(im.getItems()), 1) # Once again, but now it should be another result = im.deleteItemByOrd(0) self.assertEqual(result, True) self.assertEqual(len(im.getItems()), 0)
def hasItems(self, order): """Returns True if order has at least one item with valid url """ for item in IItemManagement(order).getItems(): if item.getProduct() is not None: return True return False
def showCheckOutButton(self): """ """ cart = self._getCart() if IItemManagement(cart).hasItems(): return True return False
def testAddItemsFromCart(self): """ """ cm = ICartManagement(self.shop) cart1 = cm.createCart() im1 = IItemManagement(cart1) im1.addItem(self.product_1, properties=()) im1.addItem(self.product_2, properties=(), quantity=3) self.login("newmember") cart2 = cm.createCart() im2 = IItemManagement(cart2) im2.addItemsFromCart(cart1) self.assertEqual(len(im2.getItems()), 2)
def testHasItemsAsAdmin(self): """ """ cm = ICartManagement(self.shop) cart = cm.createCart() # This caused an error as shasattr was not yet used within # CartManagement.getCart(). It returned an ATFolder. im = IItemManagement(cart)
def deleteItem(self): """ """ toDeleteItem = self.context.REQUEST.get("toDeleteItem") cart = ICartManagement(self.context).getCart() IItemManagement(cart).deleteItem(toDeleteItem) return self._render()
def getLatestOrder(self): """Returns the last order id of authenticated customer """ om = IOrderManagement(self.context) orders = om.getOrdersForAuthenticatedCustomer() orders.sort(lambda a, b: cmp(b.created(), a.created())) order = orders[0] # Address customer = order.getCustomer() address = IAddressManagement(customer).getInvoiceAddress() prices = IPrices(order) transaction = { "order_id": order.getId(), "affiliation": "", "total": prices.getPriceForCustomer(), "tax": (prices.getPriceForCustomer() - prices.getPriceNet()), "shipping": order.getShippingPriceGross(), "city": address.city, "state": "", "country": address.country_title(), } items = [] for item in IItemManagement(order).getItems(): # Product product = item.getProduct() # Category try: category = product.getCategories()[0] category_title = category.Title() except IndexError: category_title = u"" items.append({ "order_id": order.getId(), "sku": product.getArticleId(), "productname": product.Title(), "category": category_title, "price": item.getProductPriceGross(), "quantity": item.getProductQuantity() }) result = { "id": order.getId(), "url": order.absolute_url(), "google_transaction": transaction, "google_items": items, } return result
def testGetItems(self): """ """ self.order.invokeFactory("OrderItem", "item_1") self.order.invokeFactory("OrderItem", "item_2") im = IItemManagement(self.order) item_ids = [item.getId() for item in im.getItems()] self.assertEqual(item_ids, ["item_1", "item_2"])
def afterSetUp(self): """ """ super(TestCartTaxes, self).afterSetUp() cm = ICartManagement(self.shop) self.cart = cm.createCart() im = IItemManagement(self.cart) im.addItem(self.product_1, properties=(), quantity=2) im.addItem(self.product_2, properties=(), quantity=3)
def getAmountOfShops(self): """ """ cart = self.getCart() shops = {} for item in IItemManagement(cart).getItems(): shop = IShopManagement(item.getProduct()).getShop() shops[shop.UID()] = 1 return len(shops.keys())
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 getAmountOfArticles(self): """ """ cart = self._getCart() if cart is None: return 0 amount = 0 for item in IItemManagement(cart).getItems(): amount += item.getAmount() return amount
def showCheckOutLink(self): """ """ cart = self._getCart() if cart is None: return False if IItemManagement(cart).hasItems() == False: return False return True
def testHasItems(self): """ """ self.logout() cm = ICartManagement(self.shop) cart = cm.createCart() im = IItemManagement(cart) self.assertEqual(im.hasItems(), False) im.addItem(self.product_1, properties=[]) self.assertEqual(im.hasItems(), True)
def hasCartItems(self): """ """ cart = self._getCart() if cart is None: return False im = IItemManagement(cart) if im.hasItems(): return True return False
def testDeleteItemByOrd(self): """ """ self.login("newmember") view = getMultiAdapter((self.shop.products.product_1, self.shop.products.product_1.REQUEST), name="addToCart") view.addToCart() cart = ICartManagement(self.shop).getCart() im = IItemManagement(cart) im.deleteItemByOrd(0) self.failIf(hasattr(cart, "0"))
def testAddProductAsMember(self): """ """ self.login("newmember") view = getMultiAdapter((self.shop.products.product_2, self.shop.products.product_2.REQUEST), name="addToCart") view.addToCart() cart = ICartManagement(self.shop).getCart() items = IItemManagement(cart).getItems() self.assertEqual(len(items), 1) view.addToCart() items = IItemManagement(cart).getItems() self.assertEqual(len(items), 1) view = getMultiAdapter((self.shop.products.product_1, self.shop.products.product_1.REQUEST), name="addToCart") view.addToCart() items = IItemManagement(cart).getItems() self.assertEqual(len(items), 2)
def getItems(self): """ """ nc = queryUtility(INumberConverter) cm = ICurrencyManagement(self.context) items = [] item_manager = IItemManagement(self.context) for item in item_manager.getItems(): product_price_gross = cm.priceToString(item.getProductPriceGross(), suffix=None) tax_rate = nc.floatToTaxString(item.getTaxRate()) tax = cm.priceToString(item.getTax(), suffix=None) price_gross = cm.priceToString(item.getPriceGross(), suffix=None) # Get url. Takes care of, if the product has been deleted in the # meanwhile. product = item.getProduct() if product is None: url = None else: if IProductVariant.providedBy(product): url = product.aq_inner.aq_parent.absolute_url() else: url = product.absolute_url() # Properties for property in item.getProperties(): if IProductVariant.providedBy(product) == True: property["show_price"] = False else: property["show_price"] = True temp = { "product_title" : item.getProductTitle(), "product_quantity" : item.getProductQuantity(), "product_url" : url, "product_price_gross" : product_price_gross, "price_gross" : price_gross, "tax_rate" : tax_rate, "tax" : tax, "properties" : item.getProperties(), "has_discount" : abs(item.getDiscountGross()) > 0, "discount_description" : item.getDiscountDescription(), "discount" : cm.priceToString(item.getDiscountGross(), prefix="-", suffix=None), } items.append(temp) return items
def removeCart(self, cart): """ """ for cart_item in IItemManagement(cart).getItems(): product = cart_item.getProduct() # Remove only when product has not unlimited amount if product.getUnlimitedAmount() == False: amount = cart_item.getAmount() new_amount = product.getStockAmount() - amount product.setStockAmount(new_amount) if new_amount <= 0: notify(StockAmountIsZeroEvent(product))
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)