예제 #1
0
def process_promo_code(request):
    data = {}
    code = request.POST.get('code', None)
    if not code:
        data['message'] = 'Promo code not provided'
    else:
        order = cart_utils.get_pending_cart(request)
        manager = cart_utils.PromoCodeManager(request, order, code)
        if not manager.promo_code:
            data['message'] = 'Incorrect promo code'
        else:
            already_used = manager.is_already_used()
            if already_used:
                data['message'] = 'Promo code already used'
            else:
                cart_total = order.get_cart_total()
                if not manager.meets_requirements(cart_total):
                    data['message'] = f"Minimum purchase total value for this promo code is " \
                                      f"{manager.promo_code.minimum_order_value}{str(cart_total.currency)}"
                else:
                    order.apply_promo_code(manager.promo_code)
                    manager.save_code_usage()
                    order = cart_utils.get_pending_cart(request)
                    data = manager.get_context_data(order)
    return JsonResponse(data)
예제 #2
0
def delete_purchase(request):
    data = {}
    existing_cart = cart_utils.get_pending_cart(request)
    cart_deleted = existing_cart.delete()
    if cart_deleted:
        data['success'] = cart_deleted
    return JsonResponse(data)
예제 #3
0
 def check_if_exist_in_cart(request, prod):
     from cart.utils import get_pending_cart
     result = None
     cart = get_pending_cart(request)
     try:
         result = OrderItem.objects.get(order=cart,
                                        product=prod).id if cart else False
     except OrderItem.DoesNotExist:
         pass
     return result
예제 #4
0
def delete_promo_code(request):
    data = {}
    order = cart_utils.get_pending_cart(request)
    code = request.POST.get('cart_id', None)
    if order and code:
        order.promo_code = None
        order.save(update_fields=['promo_code'])
        usage = PromoCodeUsage.objects.get(order=order)
        usage.delete()
        data['success'] = True
        messages.success(request, 'Promo code has been deleted')
    return JsonResponse(data)
예제 #5
0
def calculate_item_in_cart(request):
    item_id = request.POST.get('item_id', None)
    new_cart_value = request.POST.get('cart_value', None)

    # create empty dictionary for JsonResponse data
    data = {}

    # return if parameters not passed
    if item_id is None and new_cart_value is None:
        return JsonResponse(data)

    # get current logged-in user order
    existing_cart = cart_utils.get_pending_cart(request)

    try:
        # check if new cart value and item_id are integers
        value = int(new_cart_value)
        item_id = int(item_id)

        # get current logged-in user order and order items objects in cart
        updated_item = OrderItem.objects.get(order=existing_cart,
                                             product_id=item_id)

    # render checkout site if request's arguments aren't integers
    # or if order item objects don't exist
    except (ValueError, OrderItem.DoesNotExist):
        return JsonResponse(data)

    # check if value is in the right range
    if 0 < value <= MAX_ITEMS_IN_CART:
        updated_item.amount = value
        updated_item.save(update_fields=['amount'])
        data['success'] = True

        # after successful update more data for JsonResponse
        data['amount'] = str(updated_item.amount)
        data['item_total_value'] = str(updated_item.get_item_total())
        if updated_item.is_discounted():
            data['old_price_checkout'] = str(
                updated_item.get_item_total_no_discount())
        data['cart_total_value'] = str(existing_cart.get_cart_total())
        data['cart_qty'] = str(existing_cart.get_cart_qty())
    else:
        messages.error(
            request, 'Incorrect product amount (max {} items)'.format(
                MAX_ITEMS_IN_CART))
        data['success'] = False

    return JsonResponse(data)
예제 #6
0
def delete_item_from_cart(request):
    item_id = request.POST.get('item_id', None)

    # create empty dictionary for JsonResponse data
    data = {}

    existing_cart = cart_utils.get_pending_cart(request)

    # return if parameters not passed
    if item_id is None or existing_cart is None:
        return JsonResponse(data)

    try:
        # check if new cart value and item_id are integers
        item_id = int(item_id)

    # render checkout site if request's argument isn't integer
    except ValueError:
        return JsonResponse(data)

    try:
        OrderItem.objects.get(order=existing_cart,
                              product__id=item_id).delete()
    except OrderItem.DoesNotExist:
        data['success'] = False
    else:
        if existing_cart.items.first():
            data['success'] = True
            data['cart_total_value'] = str(existing_cart.get_cart_total())
            data['cart_qty'] = str(existing_cart.get_cart_qty())

        # remove cart if last product is deleted
        else:
            remove_cart = existing_cart.delete()
            data['remove_cart'] = remove_cart
    return JsonResponse(data)
예제 #7
0
def cart(request):
    return render(request, 'cart/cart.html', {'order': get_pending_cart(request), })
예제 #8
0
def add_item_to_cart(request):
    item_id = request.POST.get('item_id', None)

    # create dictionary for JsonResponse data
    data = {'success': False}

    product = Product.objects.filter(id=item_id).first()

    if item_id and product:
        existing_cart = cart_utils.get_pending_cart(request)
        data['new_item'] = True
        if existing_cart:
            order_item = existing_cart.items.filter(product=product).first()
            if order_item:
                order_item.amount = order_item.amount + 1
                order_item.save()
                data['new_item'] = False
                data['success'] = True
                data['cart_qty'] = existing_cart.get_cart_qty()
                data['amount'] = str(order_item.amount)
            else:
                order_item = OrderItem.objects.create(product=product)

                if order_item:
                    existing_cart.items.add(order_item)
                    existing_cart.save()
                    data['success'] = True
                    data['cart_qty'] = existing_cart.get_cart_qty()
        else:
            if request.user.id:
                user = get_object_or_404(User, id=request.user.id)
                cart, status = Order.objects.get_or_create(owner=user,
                                                           is_ordered=False)
            else:
                if not request.session.session_key:
                    request.session['guest'] = True
                    request.session.save()
                session = get_object_or_404(
                    Session, session_key=request.session.session_key)
                cart, status = Order.objects.get_or_create(session_key=session,
                                                           is_ordered=False)
            cart.ref_code = cart_utils.generate_order_id()
            if status:
                order_item = OrderItem.objects.create(product=product)
                cart.items.add(order_item)
                cart.save()
                existing_cart = cart
                data['success'] = True
                data['cart_qty'] = '1'
                data['new_cart'] = True
                data['cart_url'] = 'https://' + str(
                    request.get_host()) + reverse('cart')

        data['cart_total_value'] = str(existing_cart.get_cart_total())
        data['item_id'] = item_id
        if data['new_item']:
            data['product_url'] = str(product.get_absolute_url())
            data['product_thumbnail_url'] = 'https://' + str(
                request.get_host()) + str(product.thumbnail.url)
            data['product_subdepartment_name'] = product.subdepartment.name
            data['product_name'] = product.name
            data['product_price'] = str(
                product.price if product.discounted_price is None else product.
                discounted_price)
        # messages.info(request, "Product {} added to cart".format(product.name))
    return JsonResponse(data)