Example #1
0
    def getShippingMethods(self):
        """
        """
        customer = ICustomerManagement(self.context).getAuthenticatedCustomer()
        selected_shipping_id = customer.selected_shipping_method

        sm = IShippingMethodManagement(self.context)

        shipping_methods = []
        for shipping in sm.getShippingMethods(check_validity=True):

            if selected_shipping_id == safe_unicode(shipping.getId()):
                checked = True
            elif selected_shipping_id == u"" and shipping.getId() == "default":
                checked = True
            else:
                checked = False

            shipping_methods.append({
                "id": shipping.getId(),
                "title": shipping.Title,
                "description": shipping.Description,
                "checked": checked,
            })

        return shipping_methods
Example #2
0
    def isValid(self, product=None):
        """Returns True if the selected shipping method of the current customer
        is within selected shipping methods of this criterion.
        """
        shop = IShopManagement(self.context).getShop()
        sm = IShippingMethodManagement(shop)
        selected_method = sm.getSelectedShippingMethod()

        if self.context.getOperator() == "current":
            if selected_method is not None and \
               selected_method.getId() in self.context.getShippingMethods():
                return True
            else:
                return False
        else:
            for id in self.context.getShippingMethods():

                # Never check validity for itself (context)
                if id == self.context.getId():
                    continue

                shipping_method = sm.getShippingMethod(id)
                if IValidity(shipping_method).isValid() == True:
                    return False
            return True
 def testGetSelectedShippingMethod(self):
     """
     """
     sm = IShippingMethodManagement(self.shop)
     result = sm.getSelectedShippingMethod()
     
     self.assertEqual(result.getId(), "standard")
    def testGetSelectedShippingMethod(self):
        """
        """
        sm = IShippingMethodManagement(self.shop)
        result = sm.getSelectedShippingMethod()

        self.assertEqual(result.getId(), "standard")
Example #5
0
 def isValid(self, product=None):
     """Returns True if the selected shipping method of the current customer
     is within selected shipping methods of this criterion.
     """        
     shop = IShopManagement(self.context).getShop()
     sm = IShippingMethodManagement(shop)
     selected_method = sm.getSelectedShippingMethod()
     
     if self.context.getOperator() == "current":
         if selected_method is not None and \
            selected_method.getId() in self.context.getShippingMethods():
             return True
         else:
             return False
     else:
         for id in self.context.getShippingMethods():
             
             # Never check validity for itself (context)
             if id == self.context.getId():
                 continue
                 
             shipping_method = sm.getShippingMethod(id)
             if IValidity(shipping_method).isValid() == True:
                 return False
         return True
             
    def testGetShippingMethod_2(self):
        """Requested shipping method doesn't exist.
        """
        sm = IShippingMethodManagement(self.shop)
        result = sm.getShippingMethod("dummy")

        self.failUnless(result is None)
    def testGetShippingMethod_1(self):
        """Requested shipping method exists.
        """
        sm = IShippingMethodManagement(self.shop)
        result = sm.getShippingMethod("standard")

        self.assertEqual(result.getId(), "standard")
    def testGetShippingMethod_2(self):
        """Requested shipping method doesn't exist.
        """
        sm = IShippingMethodManagement(self.shop)
        result = sm.getShippingMethod("dummy")

        self.failUnless(result is None)
    def testGetShippingMethod_1(self):
        """Requested shipping method exists.
        """
        sm = IShippingMethodManagement(self.shop)
        result = sm.getShippingMethod("standard")        

        self.assertEqual(result.getId(), "standard")
Example #10
0
    def getShippingMethods(self):
        """
        """
        customer = ICustomerManagement(self.context).getAuthenticatedCustomer()
        selected_shipping_id = customer.selected_shipping_method
        
        sm = IShippingMethodManagement(self.context)
        
        shipping_methods = []
        for shipping in sm.getShippingMethods():

            if selected_shipping_id == safe_unicode(shipping.getId()):
                checked = True
            elif selected_shipping_id == u"" and shipping.getId() == "standard":
                checked = True
            else:
                checked = False
                            
            shipping_methods.append({
                "id" : shipping.getId(),
                "title" : shipping.Title,
                "description" : shipping.Description,
                "checked" : checked,
            })
            
        return shipping_methods
    def testGetShippingMethods(self):
        """
        """
        sm = IShippingMethodManagement(self.shop)
        methods = sm.getShippingMethods()
        ids = [method.getId() for method in methods]

        self.assertEqual(ids, ["standard"])
    def testGetShippingMethods(self):
        """
        """
        sm = IShippingMethodManagement(self.shop)
        methods = sm.getShippingMethods()
        ids = [method.getId() for method in methods]

        self.assertEqual(ids, ["standard"])
Example #13
0
    def _getShippingMethodsAsDL(self):
        """Returns all payment methods as DisplayList
        """
        dl = DisplayList()

        shop = IShopManagement(self).getShop()
        sm = IShippingMethodManagement(shop)

        for shipping_method in sm.getShippingMethods():
            # Don't display the parent shipping method
            if shipping_method == self.aq_parent:
                continue
            dl.add(shipping_method.getId(), shipping_method.Title())

        return dl
Example #14
0
    def _getShippingMethodsAsDL(self):
        """Returns all payment methods as DisplayList
        """
        dl = DisplayList()
        
        shop = IShopManagement(self).getShop()
        sm = IShippingMethodManagement(shop)
        
        for shipping_method in sm.getShippingMethods():
            # Don't display the parent shipping method
            if shipping_method == self.aq_parent:
                continue
            dl.add(shipping_method.getId(), shipping_method.Title())

        return dl
Example #15
0
    def getShippingInfo(self):
        """
        """
        sm = IShippingPriceManagement(self.context)
        shipping_price = sm.getPriceForCustomer()

        cm = ICurrencyManagement(self.context)
        price = cm.priceToString(shipping_price)
        method = IShippingMethodManagement(
            self.context).getSelectedShippingMethod()

        return {
            "price": price,
            "title": method.Title(),
            "description": method.Description()
        }
Example #16
0
    def getShippingMethods(self):
        """
        """
        shop = IShopManagement(self.context).getShop()
        pm = IShippingMethodManagement(shop)
                
        result = []
        for shipping_method in pm.getShippingMethods():
            result.append({
            
                "id"       : shipping_method.getId(),            
                "title"    : shipping_method.Title(),
                "url"      : shipping_method.absolute_url(),
                "up_url"   : "%s/es_folder_position?position=up&id=%s" % (self.context.absolute_url(), shipping_method.getId()),
                "down_url" : "%s/es_folder_position?position=down&id=%s" % (self.context.absolute_url(), shipping_method.getId()),
                "amount_of_criteria" : self._getAmountOfCriteria(shipping_method.getId())
            })

        return result
Example #17
0
    def getShippingInfo(self):
        """
        """
        sm = IShippingPriceManagement(self.context)
        shipping_price = sm.getPriceForCustomer()

        cm = ICurrencyManagement(self.context)
        price = cm.priceToString(shipping_price, suffix=None)
        method = IShippingMethodManagement(
            self.context).getSelectedShippingMethod()

        if method is None:
            return {
                "display": False,
            }
        else:
            return {
                "price": price,
                "title": method.Title(),
                "description": method.Description(),
                "display": len(self.getCartItems()) > 0
            }
Example #18
0
    def getShippingMethods(self):
        """
        """
        customer = ICustomerManagement(self.context).getAuthenticatedCustomer()
        selected_shipping_id = customer.selected_shipping_method

        # If the available shipping methods has been changed we must update the 
        # selected shipping method of the customer 
        shop = IShopManagement(self.context).getShop()
        shipping_methods = IShippingMethodManagement(shop).getShippingMethods(check_validity=True)
        shipping_methods_ids = [sm.getId() for sm in shipping_methods]

        # Set selected shipping method
        if selected_shipping_id not in shipping_methods_ids:
            customer.selected_shipping_method = shipping_methods_ids[0]
        
        sm = IShippingMethodManagement(self.context)
        
        shipping_methods = []
        for shipping in sm.getShippingMethods(check_validity=True):

            if selected_shipping_id == safe_unicode(shipping.getId()):
                checked = True
            elif selected_shipping_id == u"" and shipping.getId() == "default":
                checked = True
            else:
                checked = False
                            
            shipping_methods.append({
                "id" : shipping.getId(),
                "title" : shipping.Title,
                "description" : shipping.Description,
                "checked" : checked,
            })
            
        return shipping_methods        
Example #19
0
    def getShippingMethods(self):
        """
        """
        customer = ICustomerManagement(self.context).getAuthenticatedCustomer()
        selected_shipping_id = customer.selected_shipping_method

        # If the available shipping methods has been changed we must update the
        # selected shipping method of the customer
        shop = IShopManagement(self.context).getShop()
        shipping_methods = IShippingMethodManagement(shop).getShippingMethods(
            check_validity=True)
        shipping_methods_ids = [sm.getId() for sm in shipping_methods]

        # Set selected shipping method
        if selected_shipping_id not in shipping_methods_ids:
            customer.selected_shipping_method = shipping_methods_ids[0]

        sm = IShippingMethodManagement(self.context)

        shipping_methods = []
        for shipping in sm.getShippingMethods(check_validity=True):

            if selected_shipping_id == safe_unicode(shipping.getId()):
                checked = True
            elif selected_shipping_id == u"" and shipping.getId() == "default":
                checked = True
            else:
                checked = False

            shipping_methods.append({
                "id": shipping.getId(),
                "title": shipping.Title,
                "description": shipping.Description,
                "checked": checked,
            })

        return shipping_methods
Example #20
0
    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
Example #21
0
    def _refreshCart(self):
        """
        """
        customer = ICustomerManagement(self.context).getAuthenticatedCustomer()

        cart = ICartManagement(self.context).getCart()
        if cart is None:
            return

        # Collect cart item properties for lookup
        selected_properties = {}
        for key, value in self.context.request.items():
            if key.startswith("property_"):
                property_id, cart_item_id = key.split(":")
                property_id = property_id[42:]

                if selected_properties.has_key(cart_item_id) == False:
                    selected_properties[cart_item_id] = []

                selected_properties[cart_item_id].append({
                    "id":
                    property_id,
                    "selected_option":
                    value
                })

        im = IItemManagement(cart)

        total_items = 0
        i = 1
        for cart_item in im.getItems():
            ci = "cart_item_%s" % i
            amount = self.context.REQUEST.get(ci)

            try:
                amount = int(amount)
            except ValueError:
                continue

            if amount < 0:
                continue

            if amount == 0:
                im.deleteItemByOrd(i - 1)
            else:
                cart_item.setAmount(amount)
            i += 1

            # Set properties
            product = cart_item.getProduct()
            if IProductVariant.providedBy(product):
                product = product.aq_inner.aq_parent
                pvm = IProductVariantsManagement(product)

                # We need the properties also as dict to get the selected
                # variant. Feels somewhat dirty. TODO: Try to unify the data
                # model for properties.
                properties = {}
                for property in selected_properties[cart_item.getId()]:
                    properties[property["id"]] = property["selected_option"]

                variant = pvm.getSelectedVariant(properties)
                cart_item.setProduct(variant)

                # TODO: At the moment we have to set the properties of the cart
                # item too. This is used in checkout-order-preview. Think about
                # to get rid of this because the properties are already available
                # in the variant.
                cart_item.setProperties(selected_properties[cart_item.getId()])

            else:
                if selected_properties.has_key(cart_item.getId()):
                    cart_item.setProperties(
                        selected_properties[cart_item.getId()])

        # Set selected country global and within current selected invoice
        # address. Why? If a customer delete all addresses the current selected
        # country is still saved global and can be used to calculate the
        # shipping price.
        selected_country = safe_unicode(self.request.get("selected_country"))
        customer.selected_country = selected_country
        #invoice_address = IAddressManagement(customer).getInvoiceAddress()
        #if invoice_address is not None:
        #    invoice_address.country = selected_country
        shipping_address = IAddressManagement(customer).getShippingAddress()
        if shipping_address is not None:
            shipping_address.country = queryUtility(IIDNormalizer).normalize(
                selected_country)

        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]

        # Set selected payment method type
        customer.selected_payment_method = \
            safe_unicode(self.request.get("selected_payment_method"))

        # Set selected VAT registration
        selected_vat_country = safe_unicode(
            self.request.get("selected_vat_country"))
        selected_vat_number = safe_unicode(
            self.request.get("selected_vat_number"))
        if selected_vat_country == "" or selected_vat_country is None or selected_vat_number is None:
            customer.vatreg = None
        elif selected_vat_country == "XX":
            customer.vatreg = selected_vat_country
        else:
            customer.vatreg = selected_vat_country + selected_vat_number