def refresh_cart(request): """Refreshes the cart after some changes has been taken place: the amount of a product or shipping/payment method. """ cart = cart_utils.get_cart(request) customer = customer_utils.get_or_create_customer(request) # Update country country = request.POST.get("country") if customer.selected_shipping_address: customer.selected_shipping_address.country_id = country customer.selected_shipping_address.save() if customer.selected_invoice_address: customer.selected_invoice_address.country_id = country customer.selected_invoice_address.save() customer.selected_country_id = country # NOTE: The customer has to be saved already here in order to calculate # a possible new valid shippig method below, which coulb be triggered by # the changing of the shipping country. customer.save() # Update Amounts for item in cart.items(): amount = request.POST.get("amount-cart-item_%s" % item.id, 0) try: item.amount = int(amount) except ValueError: item.amount = 1 item.save() # IMPORTANT: We have to send the signal already here, because the valid # shipping methods might be dependent on the price. cart_changed.send(cart, request=request) # Update shipping method customer.selected_shipping_method_id = request.POST.get("shipping_method") valid_shipping_methods = shipping_utils.get_valid_shipping_methods(request) if customer.selected_shipping_method not in valid_shipping_methods: customer.selected_shipping_method = shipping_utils.get_default_shipping_method(request) # Update payment method customer.selected_payment_method_id = request.POST.get("payment_method") # Last but not least we save the customer ... customer.save() return HttpResponse(cart_inline(request))
def delete_shipping_method(request, shipping_method_id): """Deletes shipping method with passed shipping id. All customers, which have selected this shipping method are getting the default shipping method. """ try: shipping_method = ShippingMethod.objects.get(pk=shipping_method_id) except ObjectDoesNotExist: pass else: for customer in Customer.objects.filter(selected_shipping_method=shipping_method_id): customer.selected_shipping_method = shipping_utils.get_default_shipping_method(request) customer.save() shipping_method.delete() return lfs.core.utils.set_message_cookie( url=reverse("lfs_manage_shipping"), msg=_(u"Shipping method has been deleted.") )
def delete_shipping_method(request, shipping_method_id): """Deletes shipping method with passed shipping id. All customers, which have selected this shipping method are getting the default shipping method. """ try: shipping_method = ShippingMethod.objects.get(pk=shipping_method_id) except ObjectDoesNotExist: pass else: for customer in Customer.objects.filter(selected_shipping_method=shipping_method_id): customer.selected_shipping_method = shipping_utils.get_default_shipping_method(request) customer.save() shipping_method.delete() return lfs.core.utils.set_message_cookie( url=reverse("lfs_manage_shipping"), msg=_(u"Shipping method has been deleted."), )
def refresh_cart(request): """ Refreshes the cart after some changes has been taken place, e.g.: the amount of a product or shipping/payment method. """ cart = cart_utils.get_cart(request) if not cart: raise Http404 customer = customer_utils.get_or_create_customer(request) # Update country country_iso = request.POST.get("country") if country_iso: selected_country = Country.objects.get(code=country_iso.lower()) customer.selected_country_id = selected_country.id if customer.selected_shipping_address: customer.selected_shipping_address.country = selected_country customer.selected_shipping_address.save() customer.selected_shipping_address.save() if customer.selected_invoice_address: customer.selected_invoice_address.country = selected_country customer.selected_invoice_address.save() customer.selected_invoice_address.save() # NOTE: The customer has to be saved already here in order to calculate # a possible new valid shippig method below, which coulb be triggered by # the changing of the shipping country. customer.save() # Update Amounts message = "" for item in cart.get_items(): amount = request.POST.get("amount-cart-item_%s" % item.id, "0.0") amount = item.product.get_clean_quantity_value(amount, allow_zero=True) if item.product.manage_stock_amount and amount > item.product.stock_amount and not item.product.order_time: amount = item.product.stock_amount if amount < 0: amount = 0 if amount == 0: message = _( u"Sorry, but '%(product)s' is not available anymore." % {"product": item.product.name}) elif amount == 1: message = _( u"Sorry, but '%(product)s' is only one time available." % {"product": item.product.name}) else: message = _( u"Sorry, but '%(product)s' is only %(amount)s times available." ) % { "product": item.product.name, "amount": amount } if item.product.get_active_packing_unit(): item.amount = item.product.get_amount_by_packages(float(amount)) else: item.amount = amount if amount == 0: item.delete() else: item.save() # IMPORTANT: We have to send the signal already here, because the valid # shipping methods might be dependent on the price. cart_changed.send(cart, request=request) # Update shipping method shipping_method = get_object_or_404(ShippingMethod, pk=request.POST.get("shipping_method")) customer.selected_shipping_method = shipping_method valid_shipping_methods = shipping_utils.get_valid_shipping_methods(request) if customer.selected_shipping_method not in valid_shipping_methods: customer.selected_shipping_method = shipping_utils.get_default_shipping_method( request) # Update payment method payment_method = get_object_or_404(PaymentMethod, pk=request.POST.get("payment_method")) customer.selected_payment_method = payment_method # Last but not least we save the customer ... customer.save() result = json.dumps({ "html": cart_inline(request), "message": message, }, cls=LazyEncoder) return HttpResponse(result, content_type='application/json')
def refresh_cart(request): """ Refreshes the cart after some changes has been taken place, e.g.: the amount of a product or shipping/payment method. """ cart = cart_utils.get_cart(request) if not cart: raise Http404 customer = customer_utils.get_or_create_customer(request) # Update country country_iso = request.POST.get("country") if country_iso: selected_country = Country.objects.get(code=country_iso.lower()) customer.selected_country_id = selected_country.id if customer.selected_shipping_address: customer.selected_shipping_address.country = selected_country customer.selected_shipping_address.save() customer.selected_shipping_address.save() if customer.selected_invoice_address: customer.selected_invoice_address.country = selected_country customer.selected_invoice_address.save() customer.selected_invoice_address.save() # NOTE: The customer has to be saved already here in order to calculate # a possible new valid shippig method below, which coulb be triggered by # the changing of the shipping country. customer.save() # Update Amounts message = "" for item in cart.get_items(): amount = request.POST.get("amount-cart-item_%s" % item.id, "0.0") amount = item.product.get_clean_quantity_value(amount, allow_zero=True) if item.product.manage_stock_amount and amount > item.product.stock_amount and not item.product.order_time: amount = item.product.stock_amount if amount < 0: amount = 0 if amount == 0: message = _(u"Sorry, but '%(product)s' is not available anymore." % {"product": item.product.name}) elif amount == 1: message = _(u"Sorry, but '%(product)s' is only one time available." % {"product": item.product.name}) else: message = _(u"Sorry, but '%(product)s' is only %(amount)s times available.") % { "product": item.product.name, "amount": amount, } if item.product.get_active_packing_unit(): item.amount = item.product.get_amount_by_packages(float(amount)) else: item.amount = amount if amount == 0: item.delete() else: item.save() # IMPORTANT: We have to send the signal already here, because the valid # shipping methods might be dependent on the price. cart_changed.send(cart, request=request) # Update shipping method shipping_method = get_object_or_404(ShippingMethod, pk=request.POST.get("shipping_method")) customer.selected_shipping_method = shipping_method valid_shipping_methods = shipping_utils.get_valid_shipping_methods(request) if customer.selected_shipping_method not in valid_shipping_methods: customer.selected_shipping_method = shipping_utils.get_default_shipping_method(request) # Update payment method payment_method = get_object_or_404(PaymentMethod, pk=request.POST.get("payment_method")) customer.selected_payment_method = payment_method # Last but not least we save the customer ... customer.save() result = json.dumps({"html": cart_inline(request), "message": message}, cls=LazyEncoder) return HttpResponse(result, content_type="application/json")
def refresh_cart(request): """ Refreshes the cart after some changes has been taken place, e.g.: the amount of a product or shipping/payment method. """ cart = cart_utils.get_cart(request) customer = customer_utils.get_or_create_customer(request) # Update country country_iso = request.POST.get("country") if country_iso: selected_country = Country.objects.get(code=country_iso.lower()) customer.selected_country_id = selected_country.id if customer.selected_shipping_address: customer.selected_shipping_address.country = selected_country customer.selected_shipping_address.save() customer.selected_shipping_address.save() if customer.selected_invoice_address: customer.selected_invoice_address.country = selected_country customer.selected_invoice_address.save() customer.selected_invoice_address.save() # NOTE: The customer has to be saved already here in order to calculate # a possible new valid shippig method below, which coulb be triggered by # the changing of the shipping country. customer.save() # Update Amounts message = "" for item in cart.get_items(): try: value = request.POST.get("amount-cart-item_%s" % item.id, "0.0") if isinstance(value, unicode): # atof() on unicode string fails in some environments, like Czech value = value.encode("utf-8") amount = locale.atof(value) except (TypeError, ValueError): amount = 1.0 if item.product.manage_stock_amount and amount > item.product.stock_amount and not item.product.order_time: amount = item.product.stock_amount if amount < 0: amount = 0 if amount == 0: message = _(u"Sorry, but '%(product)s' is not available anymore." % {"product": item.product.name}) elif amount == 1: message = _(u"Sorry, but '%(product)s' is only one time available." % {"product": item.product.name}) else: message = _(u"Sorry, but '%(product)s' is only %(amount)s times available.") % {"product": item.product.name, "amount": amount} if item.product.active_packing_unit: item.amount = item.product.get_amount_by_packages(float(amount)) else: item.amount = amount if amount == 0: item.delete() else: item.save() # IMPORTANT: We have to send the signal already here, because the valid # shipping methods might be dependent on the price. cart_changed.send(cart, request=request) # Update shipping method customer.selected_shipping_method_id = request.POST.get("shipping_method") valid_shipping_methods = shipping_utils.get_valid_shipping_methods(request) if customer.selected_shipping_method not in valid_shipping_methods: customer.selected_shipping_method = shipping_utils.get_default_shipping_method(request) # Update payment method customer.selected_payment_method_id = request.POST.get("payment_method") # Last but not least we save the customer ... customer.save() result = simplejson.dumps({ "html": cart_inline(request), "message": message, }, cls=LazyEncoder) return HttpResponse(result)
def refresh_cart(request): """ Refreshes the cart after some changes has been taken place, e.g.: the amount of a product or shipping/payment method. """ cart = cart_utils.get_cart(request) if not cart: raise Http404 customer = customer_utils.get_or_create_customer(request) # Update country country_iso = request.POST.get("country") if country_iso: selected_country = Country.objects.get(code=country_iso.lower()) customer.selected_country_id = selected_country.id if customer.selected_shipping_address: customer.selected_shipping_address.country = selected_country customer.selected_shipping_address.save() customer.selected_shipping_address.save() if customer.selected_invoice_address: customer.selected_invoice_address.country = selected_country customer.selected_invoice_address.save() customer.selected_invoice_address.save() # NOTE: The customer has to be saved already here in order to calculate # a possible new valid shippig method below, which coulb be triggered by # the changing of the shipping country. customer.save() # Update Amounts message = '' cor = '' for item in cart.get_items(): amount = request.POST.get("amount-cart-item_%s" % item.id, "0.0") amount = item.product.get_clean_quantity_value(amount, allow_zero=True) if message == '': message = _(u"%(product)s ATUALIZADO") % {"product": 'PRODUTO'} cor = "verde" if item.product.manage_stock_amount and amount > item.product.stock_amount and not item.product.order_time: amount = item.product.stock_amount message = _(u"%(product)s ATUALIZADO") % {"product": 'PRODUTO'} cor = "verde" if amount < 0: amount = 0 message = _(u"%(product)s EXCLUIDO") % {"product": 'PRODUTO'} cor = "vermelho" if amount == 0: message = _( u"Sorry, but '%(product)s' is not available anymore.") % { "product": 'PRODUTO', "amount": amount } cor = "vermelho" elif amount == 1: message = _( u"Sorry, but '%(product)s' is only one time available." ) % { "product": 'PRODUTO', "amount": amount } cor = "amarelo" else: message = _( u"Sorry, but '%(product)s' is only %(amount)s times available." ) % { "product": 'PRODUTO', "amount": amount } cor = "amarelo" if item.product.get_active_packing_unit(): item.amount = item.product.get_amount_by_packages(float(amount)) else: item.amount = amount if amount == 0: message = _(u"%(product)s EXCLUIDO") % {"product": 'PRODUTO'} cor = "vermelho" item.delete() else: item.save() # IMPORTANT: We have to send the signal already here, because the valid # shipping methods might be dependent on the price. cart_changed.send(cart, request=request) # Update shipping method shipping_method = get_object_or_404(ShippingMethod, pk=request.POST.get("shipping_method")) customer.selected_shipping_method = shipping_method valid_shipping_methods = shipping_utils.get_valid_shipping_methods(request) if customer.selected_shipping_method not in valid_shipping_methods: customer.selected_shipping_method = shipping_utils.get_default_shipping_method( request) # Update payment method payment_method = get_object_or_404(PaymentMethod, pk=request.POST.get("payment_method")) customer.selected_payment_method = payment_method # Last but not least we save the customer ... customer.save() total_quantidade = 0 total_valor = 0 for cart_item in cart.get_items(): product = cart_item.product quantidade = str(product.get_clean_quantity(cart_item.amount)) quantidade = quantidade.replace(",", ".") quantidade = float(quantidade) total_quantidade += quantidade total_valor += float(cart_item.get_price_net(request)) price = str(total_valor) if price == '0': decimal = '0' centavos = '00' else: virgula = price.find('.') decimal = price[:virgula] centavos = price[virgula:] for a in [',', '.']: decimal = decimal.replace(a, '') centavos = centavos.replace(a, '') result = json.dumps( { "html": cart_inline(request), "message": message, "cor": cor, "quantidade": str(total_quantidade), "decimal": decimal, "centavos": centavos[:2], }, cls=LazyEncoder) return HttpResponse(result, content_type='application/json')