Example #1
0
    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(),
            }        
Example #2
0
    def __init__(self, context):
        """
        """
        pvm  = IProductVariantsManagement(context)
        shop = IShopManagement(context).getShop()
        
        self.context = context

        self.gross_prices = shop.getGrossPrices()
        self.has_variants = pvm.hasVariants()
        self.taxes = ITaxes(context)

        if self.has_variants:
            self.product_variant = \
                pvm.getSelectedVariant() or pvm.getDefaultVariant()
Example #3
0
    def __init__(self, context):
        """
        """
        pvm = IProductVariantsManagement(context)
        shop = IShopManagement(context).getShop()

        self.context = context

        self.gross_prices = shop.getGrossPrices()
        self.has_variants = pvm.hasVariants()
        self.taxes = ITaxes(context)

        if self.has_variants:
            self.product_variant = \
                pvm.getSelectedVariant() or pvm.getDefaultVariant()
Example #4
0
    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(),
            }
Example #5
0
    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)
Example #6
0
    def _getPropertiesForVariants(self):
        """
        """
        u = queryUtility(INumberConverter)
        cm = ICurrencyManagement(self.context)

        selected_options = {}
        for name, value in self.request.items():
            if name.startswith("property"):
                if len(name) > 42:
                    selected_options[name[42:]] = value
                else:
                    selected_options[name[9:]] = value

        # If nothing is selected we select the default variant
        if selected_options == {}:
            pvm = IProductVariantsManagement(self.context)
            default_variant = pvm.getDefaultVariant()

            # If there is no default variant return empty list
            if default_variant is None:
                return []

            for property in default_variant.getForProperties():
                name, value = property.split(":")
                selected_options[name] = value

        pm = IPropertyManagement(self.context)

        result = []
        for property in pm.getProperties():

            # Only properties with at least one option are displayed.
            if len(property.getOptions()) == 0:
                continue

            options = []
            for option in property.getOptions():

                # generate value string
                option_id    = option["id"]
                option_name  = option["name"]
                # option_price = option["price"]
                # option_price = u.stringToFloat(option_price)
                # option_price = cm.priceToString(option_price, "long", "after")
                # content = "%s %s" % (option_name, option_price)
                content = option_name

                # is option selected?
                selected_option = selected_options.get(property.getId(), "")
                selected = option_id == selected_option

                options.append({
                    "id"       : option_id,
                    "title"    : content,
                    "selected" : selected,
                })

            result.append({
                "id"      : "property_%s_%s" % (self.context.UID(), property.getId()),
                "title"   : property.Title(),
                "options" : options,
            })

        return result