def change_quantity(request): if request.method == 'POST': cart = Cart(request) item_pk = request.POST['pk'] quantity = request.POST['quantity'] cart.change_quantity(item_pk, quantity) return HttpResponseRedirect(redirect_to='/shop/cart')
def item_page(request, item_id): cat, sub_cat, brands = objects_for_nav() if request.method == 'GET': _item = Item.objects.get(pk=item_id) print(_item.category) context = { 'item': _item, 'categories': cat, 'sub_categories': sub_cat, 'brands': brands, 'cur_category': _item.category, 'cur_sub_category': _item.sub_category, 'cur_brand': _item.manufacturer, } return render(request, 'item.jinja2', context) elif request.method == 'POST': cat, sub_cat, brands = objects_for_nav() pk = request.POST['pk'] cart = Cart(request) cart.add(pk=pk) context = { 'categories': cat, 'sub_categories': sub_cat, 'brands': brands, } return render(request, 'cart.jinja2', context)
def confirm_order(request): cat, sub_cat, brands = objects_for_nav() if request.method == 'POST': address_form = DeliveryForm(request.POST) user = User.objects.get(pk=request.POST['user']) cart = Cart(request) if address_form.is_valid(): with transaction.atomic(): address = address_form.save() order = Order(delivery_address=address, user=user) order.save() item_list = cart.list_items() if not item_list: return HttpResponseRedirect('/shop/cart') else: for item in item_list: ordered_item = OrderItem( item=Item.objects.get(pk=item.obj.pk), order=order, quantity=item.quantity) ordered_item.save() order.order_items.add(ordered_item.pk) cart.empty() context = { 'order_id': order.pk, 'categories': cat, 'sub_categories': sub_cat, 'brands': brands, } return render(request, 'order_success.html', context) else: # todo template return HttpResponse(content='Invalid address') return HttpResponse(content='hello')
def update_order_cart(request): #Check if order exists order = Order.objects.filter(user=request.user, status="En cours").first() if not order: order = Order.objects.create_order(request.user, status="En cours") #Create items in order cart = Cart(request) order.empty_order() for i in cart.list_items(): wei = 0 if i.basis_weight == '': wei = 100 else: wei = i.basis_weight ort = '' if i.orientation == '': ort = Orientation.objects.first() else: ort = Orientation.objects.get(pk=i.orientation) item = Item.objects.create_item(order, i.obj, i.quantity, wei, ort) #Create payment #print('Total ='+str(order.total_amount())) order_total = int(order.total_amount()) order_total = str(order_total / 10).replace('.', '') order_total = int(order_total) return HttpResponseRedirect( 'https://www.envoiedusteak.fr/commande/resume/')
def update_cart(request): cart = Cart(request) #Check if order exists ip = get_ip(request) order_cart = OrderCart.objects.filter(status=ip).first() if not order_cart: order_cart = OrderCart.objects.create_order_cart(status=ip) #Create items in order order_cart.empty_order() for i in cart.list_items(): wei = 0 if i.basis_weight == '': wei = 100 else: wei = i.basis_weight ort = '' if i.orientation == '': ort = Orientation.objects.first() else: ort = Orientation.objects.get(pk=i.orientation) item_cart = ItemCart.objects.create_item_cart(order_cart, i.obj, i.quantity, wei, ort) order_cart.total_amount() order_cart.save()
def get_context_data(self, **kwargs): context = super(CartResume, self).get_context_data(**kwargs) context['path'] = self.request.path cart = Cart(self.request) #Check if order exists ip = get_ip(self.request) order_cart = OrderCart.objects.filter(status=ip).first() if not order_cart: order_cart = OrderCart.objects.create_order_cart(status=ip) #Create items in order order_cart.empty_order() for i in cart.list_items(): wei = 0 if i.basis_weight == '': wei = 100 else: wei = i.basis_weight ort = '' if i.orientation == '': ort = Orientation.objects.first() else: ort = Orientation.objects.get(pk=i.orientation) item_cart = ItemCart.objects.create_item_cart( order_cart, i.obj, i.quantity, wei, ort) order_cart.total_amount() context['order'] = order_cart return (context)
def post(self, request, *args, **kwargs): if not request: return HttpResponseForbidden() form = self.get_form() if form.is_valid(): self.order = form.save(commit=False) self.order.save() for item in self.cart.list_items(Cart(self.request)): OrderItem.objects.create(order=self.order, product=item.obj, price=item.obj.price, quantity=item.quantity) if self.order.pay_type == "N": self.request.session['order_id'] = self.order.id # self.cart.empty(Cart(self.request)) return redirect(reverse('payment:process')) elif self.order.pay_type == "W_R": self.receiver_email = settings.PAYPAL_RECEIVER_EMAIL send_templated_mail(template_name='order', from_email=settings.DEFAULT_FROM_EMAIL, recipient_list=[self.order.shipping_email], context={ 'order': self.order, 'receiver_email': self.receiver_email, }) self.cart.empty(Cart(self.request)) return redirect(self.get_success_url()) else: return self.form_invalid(form)
def cart_clean(request): if request.method == 'POST': cart = Cart(request) cart.empty() return HttpResponseRedirect(redirect_to='/shop/cart')
def cart_remove(request): if request.method == 'POST': item_pk = request.POST['pk'] cart = Cart(request) cart.remove(pk=item_pk) return HttpResponseRedirect(redirect_to='/shop/cart')
from django.conf.urls import url from cart.views import CartAdd, Cart, CartInfo, CartChange urlpatterns = [ url(r'^add/$', CartAdd.as_view(), name="add"), # 购物车添加 url(r'^cart/$', Cart.as_view(), name="cart"), # 购物车添加 url(r'^cartchange/$', CartChange.as_view(), name="cartchange"), # 购物车修改 url(r'^cart_info/$', CartInfo.as_view(), name="cart_info"), # 购物车显示 ]
"""super URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/1.11/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.conf.urls import url, include 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) """ from django.conf.urls import url from cart.views import Cart, ShopCart urlpatterns = [ url(r'^Cart$', Cart.as_view(), name='Cart'), # 添加购物车 url(r'^ShopCart$', ShopCart.as_view(), name='ShopCart'), # 购物车 ]
from django.conf.urls import patterns, url from cart.views import Cart, check_orders, show_orders urlpatterns = patterns('', url(r'^$', Cart.as_view(), name = 'index'), url(r'^/item/(?P<id>[0-9]+)$', Cart.as_view(), ), url(r'^/my-orders$', show_orders, name = 'show'), url(r'^/check-orders$', check_orders, name = 'check'), )
def PaymentDone(request): cart = Cart(request) cart.empty() return render(request, 'payment/done.html')
def get_context_data(self, **kwargs): context = super(PayResume, self).get_context_data(**kwargs) context['path'] = self.request.path #Check if order exists order = Order.objects.filter(user=self.request.user, status="En cours").first() if order: if not order.shipping_address: shipping_address = ShippingAddress.objects.filter( user=self.request.user).last() if shipping_address: order.shipping_address = shipping_address order.save() if not order: shipping_address = ShippingAddress.objects.filter( user=self.request.user).last() order = Order.objects.create_order(self.request.user, status="En cours") if shipping_address: order = Order.objects.get(user=self.request.user, status="En cours") order.shipping_address = shipping_address order.save() order.use_precise_for_cf = False order.use_precise_for_stock = False #Create items in order cart = Cart(self.request) order.empty_order() for i in cart.list_items(): wei = 0 if i.basis_weight == '': wei = 100 else: wei = i.basis_weight ort = '' if i.orientation == '': ort = Orientation.objects.first() else: ort = Orientation.objects.get(pk=i.orientation) item = Item.objects.create_item(order, i.obj, i.quantity, wei, ort) #Create payment #print('Total ='+str(order.total_amount())) order_total = int(order.total_amount()) #order_total = str(order_total/10).replace('.', '') #order_total = int(order_total) try: credit = Credit.objects.get(user=self.request.user) except: credit = '' context['credit'] = credit user = self.request.user #print(self.request.user) context['billing_form'] = ShippingAddressForm(user) context['shipping_form'] = BillingAddressForm(user) context['form'] = OrderForm(user) context['order'] = order return (context)