def action_add_to_cart(self, resource, context, form): """ Add to cart """ cart = ProductCart(context) # Check if we can add to cart if not resource.is_buyable(context): msg = MSG(u"This product isn't buyable") return context.come_back(msg) # Get purchase options declination = None kw = {} for key in resource.get_purchase_options_schema(): if form[key] is not None: kw[key] = form[key] if kw: declination = resource.get_declination(kw) if declination is None: context.message = ERROR(u'Declination not exist') return # Check if product is in stock cart_quantity = cart.get_product_quantity_in_cart(resource.name) total_quantity = cart_quantity + form['quantity'] if not resource.is_in_stock_or_ignore_stock(total_quantity, declination): msg = u"Quantity in stock insufficient." return context.come_back(MSG(msg)) # Add to cart cart.add_product(resource, form['quantity'], declination) # Information message context.message = INFO(u'Product added to cart !')
def get_namespace(self, resource, context): show_if_empty = resource.get_property('show_if_empty') cart = ProductCart(context) if (self.is_admin(resource, context) is False and cart.get_nb_products() == 0 and show_if_empty is False): self.set_view_is_empty(True) default_order_title = MSG(u'Commander') return merge_dicts( cart.get_namespace(resource), order_title=resource.get_property('order_title') or default_order_title, title=resource.get_title())
def get_namespace(self, resource, context): cart = ProductCart(context) nb_products = cart.get_nb_products() shop = get_shop(context.resource) # Get total price total = cart.get_total_price(shop, with_delivery=False) if shop.show_ht_price(context): total = total['without_tax'] else: total = total['with_tax'] # Return namespace return {'nb_products': nb_products, 'total': total, 'txts_class': 'hidden' if nb_products < 2 else '', 'txt_class': 'hidden' if nb_products > 1 else ''}
def get_namespace(self, resource, context): cart = ProductCart(context) nb_products = cart.get_nb_products() shop = get_shop(context.resource) # Get total price total = cart.get_total_price(shop, with_delivery=False) if shop.show_ht_price(context): total = total['without_tax'] else: total = total['with_tax'] # Return namespace return { 'nb_products': nb_products, 'total': total, 'txts_class': 'hidden' if nb_products < 2 else '', 'txt_class': 'hidden' if nb_products > 1 else '' }
def get_tax_value(self, prefix=None): shop = get_shop(self) if prefix is None: prefix = self.get_price_prefix() # Get zone from cookie id_zone = ProductCart(get_context()).id_zone # If not define... get default zone if id_zone is None: id_zone = shop.get_property('shop_default_zone') # Check if zone has tax ? zones = shop.get_resource('countries-zones').handler zone_record = zones.get_record(int(id_zone)) if zones.get_record_value(zone_record, 'has_tax') is True: tax = self.get_property('%stax' % prefix) tax_value = TaxesEnumerate.get_value(tax) or decimal(0) return (tax_value / decimal(100) + 1) return decimal(1)
def get_price(self, country, list_weight): list_weight = deepcopy(list_weight) shop = get_shop(self) # Is Free ? if self.get_property('is_free'): return decimal(0) # Transform country to zone countries = shop.get_resource('countries').handler country_record = countries.get_record(int(country)) if countries.get_record_value(country_record, 'enabled') is False: return None zone = countries.get_record_value(country_record, 'zone') # XXX to refactor # Max value mode = self.get_property('mode') if mode == 'weight': list_weight.sort(reverse=True) list_values = list_weight elif mode == 'quantity': list_values = [1] * len(list_weight) # XXX limit by models only_this_models = self.get_property('only_this_models') if only_this_models: cart = ProductCart(get_context()) for product_cart in cart.products: product = shop.get_resource(product_cart['name'], soft=True) if product.get_property('product_model') not in only_this_models: return None # Get corresponding weight/quantity in table of price list_price_ok = {} prices = self.get_resource('prices').handler min_value = 0 for record in prices.search(PhraseQuery('zone', zone), sort_by='max-%s' % mode): max_value = prices.get_record_value(record, 'max-%s' % mode) price = prices.get_record_value(record, 'price') list_price_ok[max_value] = {'min': min_value, 'max': max_value, 'price': price} min_value = max_value # No price ? if len(list_price_ok.keys()) == 0: return None # Check all weigh if < max_weight if mode == 'weight': max_value = max(list_price_ok.keys()) for key in list_weight: if key > max_value: return None # On crée une partition de poids current_value = decimal(0) partition = [] list_values.sort(reverse=True) while list_values: if current_value + list_values[-1] <= max_value: current_value += list_values.pop() else: partition.append(current_value) current_value = decimal(0) if len(list_values) == 0: partition.append(current_value) # Calcul total price total_price = decimal(0) for p in partition: for value in list_price_ok.values(): if p >= value['min'] and p <= value['max']: total_price += value['price'] break # Add insurance if self.get_property('insurance') > decimal(0): context = get_context() cart = ProductCart(context) products_price = cart.get_total_price(shop, with_delivery=False, pretty=False) percent = self.get_property('insurance') / 100 total_price += products_price['with_tax'] * percent return total_price