def change_product_quantity_in_stock_depends_on_availability( product: Product, cart_actual_quantity: int, cart_quantity_to_change: int) -> Product: if not product.is_in_stock: raise serializers.ValidationError("Product is not available now!") if product.quantity_in_stock - cart_quantity_to_change < 0: raise serializers.ValidationError( f"You can order only {product.quantity_in_stock}") product.quantity_in_stock += cart_actual_quantity - cart_quantity_to_change if product.quantity_in_stock == 0: product.is_in_stock = False return product.save()
def get(self , request): len_cart = card_len(request) # card len category = prod_category(Category, Subcategory) cart = load_cart_DB_to_session(int(request.session['customer_id'])) request.session['cart'] = cart cart_prod = {} try: cart = request.session.get('cart') except: cart = None if cart != None: product = [] for id, value in cart.items(): product.append(id) product.append(value) ids = list(request.session.get('cart').keys()) if ids: products = Product.get_products_by_id(ids) # user_address = Billing.get_bill_address_by_id(request.session.get("customer_id")) cart_prod = { 'category': category, 'len': len_cart, 'prod':products, 'user_address': user_address, } return render(request, 'shop/checkout.html', cart_prod) messages.error(request, "Your cart is Empty") return redirect('home')
def post(self, request): address = request.POST.get('address') phone = request.POST.get('phone') state = request.POST.get('state') zipcode = request.POST.get('zipcode') customer = request.session.get('customer') cart = request.session.get('cart') product = Product.get_product_by_id(list(cart.keys())) print(address, phone, state, zipcode, customer, cart, product) for product in product: print(cart.get(str(product.id))) order = Order(customer = Customer(id = customer), product = product, price = product.price, address = address, phone = phone, zipcode = zipcode, state = state, quantity = cart.get(str(product.id))) order.save() # after checkout cart clear-------------- request.session['cart'] = {} return redirect('cart')
def get(self, request): len_cart = card_len(request) # card len category = prod_category(Category, Subcategory) #load card from database to session storage cart = load_cart_DB_to_session(int(request.session['customer_id'])) request.session['cart'] = cart try: cart = request.session.get('cart') except: cart = None if cart != None: ids = list(cart.keys()) products = Product.get_products_by_id(ids) updatecart = {} for key, value in cart.items(): prod = get_object_or_404(Product, id=int(key)) if int(value[0]) > int(prod.stock): value[0] = str(prod.stock) updatecart[key] = value else: updatecart[key] = value request.session['cart'] = updatecart save_cart(request) else: products = None cart_prod = {'category': category, 'len': len_cart, 'prod': products} return render(request, 'shop/cart.html', cart_prod)
def get(self, request): # use for category showes and dainamicay work cart = request.session.get('cart') if not cart: request.session.cart = {} products = None categories = Category.get_all_categories() categoryID = request.GET.get('category') if categoryID: products = Product.get_all_products_by_categoryid(categoryID) else: products = Product.get_all_products() data = {} data['products'] = products data['categories'] = categories # print('you are : ' ,request.session.get('email')) return render(request, 'index.html', data)
def get_context_data(self, **kwargs): context = super(SearchView, self).get_context_data() query = self.request.GET['q'] clean_query = clean_number(query) parts = Part.objects.filter(clean_part_number=clean_query) additional_products = Product.get_additional_products(sku=clean_query) context.update({ 'q': query, 'parts': parts, 'additional_products': additional_products }) return context
def post(self , request): len_cart = card_len(request) # card len category = prod_category(Category, Subcategory) cart = request.session.get('cart') product = [] for id, value in cart.items(): product.append(id) product.append(value) ids = list(request.session.get('cart').keys()) if ids: products = Product.get_products_by_id(ids) name = request.POST.get('name') email = request.POST.get('email') country = request.POST.get('country') state = request.POST.get('state') address1 = request.POST.get('address1') address2 = request.POST.get('address2') city = request.POST.get('city') postal_code = request.POST.get('postal_code') phone = request.POST.get('phone') totalprice = int(request.POST.get('total_price')) user_address = Billing.get_bill_address_by_id(request.session.get("customer_id")) # save user billing address if not user_address: billing_address = Billing(user = Customer(request.session.get('customer_id')),name=name, email=email, country=country, state=state, address1=address1, address2=address2, city=city, zip_code=postal_code, phone=phone) billing_address.save() param = {'name': name, 'email': email, 'country': country, 'state': state, 'address': address1 + " " + address2, 'city': city, 'postal_code': postal_code,'phone': phone, 'total_price': totalprice} products = Product.get_products_by_id(list(cart.keys())) order_id ="" for product in products: order = Orders( customer =Customer(id=request.session.get('customer_id')), product= product,color =cart.get(str(product.id))[1], quantity=cart.get(str(product.id))[0], price=get_price(product,cart.get(str(product.id))[0]), name=name, email=email, country=country, state=state, address=address1+" "+address2, city=city, zip_code=postal_code, phone=phone) order.save() order_id += str(order.order_id) param_dict = { 'MID': 'lieNmi15357207091028', 'ORDER_ID': 'ODR-31', 'TXN_AMOUNT': '100.00', 'CUST_ID': name, 'INDUSTRY_TYPE_ID': 'Retail', 'WEBSITE': 'WEBSTAGING', 'CHANNEL_ID': 'WEB', 'CALLBACK_URL': 'http://ashudihatti.in/handlerequest/', } chucksum = Checksum.generate_checksum(param_dict, MERCHANT_KEY) param_dict['CHECKSUMHASH']= chucksum return render(request, 'shop/payment.html', {'param_dict': param_dict})
def get(self, request): ids = list(request.session.get('cart').keys()) products = Product.get_product_by_id(ids) print(products) return render(request, 'cart.html', {'products': products})