Exemplo n.º 1
0
def add_to_cart(request, product=None):
    """
    function that takes a POST request and adds a product instance to the current customer's shopping cart
    """
    post_data = request.POST.copy()
    # quantity = post_data.get('quantity', 1)  # get quantity added, return 1 if empty
    quantity = 1
    if not product:
        product_slug = post_data.get(
            'product_slug',
            '')  # get product slug from post data, return blank if empty
        product = get_object_or_404(
            Product, slug=product_slug
        )  # fetch the product or return a missing page error
    cart_products = get_cart_items(request)  # get products in cart
    product_in_cart = False
    # check to see if item is already in cart
    for cart_item in cart_products:
        if cart_item.product.id == product.id:
            cart_item.augment_quantity(
                quantity)  # update the quantity if found
            product_in_cart = True
            break
    if not product_in_cart:
        ci = CartItem()  # create and save a new cart item
        ci.product = product
        ci.quantity = quantity
        ci.cart_id = _cart_id(request)
        ci.save()
Exemplo n.º 2
0
def add_to_cart(request):
    """ function that takes a POST request and adds a product instance to the current customer's shopping cart """
    post_data = request.POST.copy()
    # get product slug from post data, return blank if empty
    #product_slug = post_data.get('product_slug','')
    # get quantity added, return 1 if empty
    quantity = post_data.get('quantity',1)
    product_id = post_data.get('product_id', 0)
    product = get_object_or_404(Product, pk = product_id)
    # fetch the product or return a missing page error
    cart_products = get_cart_items(request)
    product_in_cart = False
    # check to see if item is already in cart
    for cart_item in cart_products:
        if cart_item.product.id == product.id:
            # update the quantity if found
            cart_item.augment_quantity(quantity)
            product_in_cart = True
    if not product_in_cart:
        # create and save a new cart item
        ci = CartItem()
        ci.product = product
        ci.quantity = quantity
        ci.cart_id = _cart_id(request)
        ci.save()
Exemplo n.º 3
0
def add_to_cart(request):
    postdata = request.POST.copy()

    # get product slug from post data, return blank if empty
    product_slug = postdata.get('product_slug', '')
    # get quantity added, return 1 if empty
    quantity = postdata.get('quantity', 1)
    # fetch the product or return a missing page error
    p = get_object_or_404(Product, slug=product_slug)
    # get products in cart
    cart_products = get_cart_items(request)
    product_in_cart = False

    # check to see if item is already in cart
    for cart_item in cart_products:
        if cart_item.product.id == p.id:
            # update the quantity if found
            cart_item.augment_quantity(quantity)
            product_in_cart = True

    if not product_in_cart:
        # create and save a new cart item
        ci = CartItem()
        ci.product = p
        ci.quantity = quantity
        ci.cart_id = _cart_id(request)
        ci.save()
Exemplo n.º 4
0
def add_to_cart(request):
    postdata = request.POST.copy()
    # Получаю название заказанного продукта
    model_id = postdata.get('model_id','')
    quantity = 1
    product_in_cart = False
    # Получаю заказанный продукт
    p = get_object_or_404(Model, id=model_id)
    # Если клиент уже есть в базе
    if CartItem.objects.filter(cart_id = _cart_id(request)):
        # Получаю все продукты в корзине
        cart = CartItem.objects.get(cart_id = _cart_id(request))
        cart_products = CartProduct.objects.filter(cartitem=cart.id)
        # Проверяю есть ли такой продукт уже в корзине
        for cart_item in cart_products:
            if cart_item.product_id == p.id:
                # Если уже есть то обновляю количество
                ttt = CartProduct.objects.get(cartitem=cart,product=p.id)
                ttt.augment_quantity(quantity)
                product_in_cart = True
        # Если нету то добавляю
        if not product_in_cart:
            cart = CartItem.objects.get(cart_id = _cart_id(request))
            cp = CartProduct(cartitem = cart, product = p)
            cp.save()
    # Если клиента нету в базе то создаю его
    else:
        ci = CartItem()
        ci.cart_id = _cart_id(request)
        ci.save()

        # И добавляю его заказ в корзину
        cart = CartItem.objects.get(cart_id = _cart_id(request))
        cp = CartProduct(cartitem = cart, product = p)
        cp.save()
Exemplo n.º 5
0
def add_to_cart(request):
    postdata = request.POST.copy()

    # get product slug from post data, return blank if empty
    product_slug = postdata.get("product_slug", "")
    # get quantity added, return 1 if empty
    quantity = postdata.get("quantity", 1)
    # fetch the product or return a missing page error
    p = get_object_or_404(Product, slug=product_slug)
    # get products in cart
    cart_products = get_cart_items(request)
    product_in_cart = False

    # check to see if item is already in cart
    for cart_item in cart_products:
        if cart_item.product.id == p.id:
            # update the quantity if found
            cart_item.augment_quantity(quantity)
            product_in_cart = True

    if not product_in_cart:
        # create and save a new cart item
        ci = CartItem()
        ci.product = p
        ci.quantity = quantity
        ci.cart_id = _cart_id(request)
        ci.save()
Exemplo n.º 6
0
def add_to_cart(request):
    postdata = request.POST.copy()
    try:
        flavor_id = int(postdata.get('flavor_id', False))
        volume_id = int(postdata.get('volume_id', False))
        conc_id = int(postdata.get('conc_id', False))
        assert volume_id and (volume_id in request.volumes_data.keys()
                              ), "You are not allowed to access this page"
    except (ValueError, TypeError) as e:
        raise Http404("Sorry! Failed to add product to cart")
    except AssertionError as e:
        raise Http404(e)
    # get quantity added , return 1 if empty
    quantity = int(postdata.get('quantity', 1))
    #fetch the product or return a missing page error
    p = get_object_or_404(ProductVariant,
                          vol_id=volume_id,
                          conc_id=conc_id,
                          flavor_id=flavor_id,
                          active=True,
                          product_tmpl_id__type="product")
    # get products in cart
    cart_products = get_cart_items(request)
    # Check to see if item already in cart
    product_in_cart = False
    available_qty = get_products_availability(p.id)[str(p.id)]
    added = False
    cart_quantity = 0
    for cart_item in cart_products:
        if cart_item.product_id == p.id:
            #upddate the quantity if found
            product_in_cart = True
            cart_quantity = cart_item.quantity + quantity
            if cart_quantity <= available_qty.get('virtual_available', 0):
                cart_item.augment_quantity(quantity)
                added = True

    if not product_in_cart:
        cart_quantity = quantity
        if cart_quantity <= available_qty.get('virtual_available', 0):
            #create and save a new cart item
            ci = CartItem()
            ci.product_id = p.id
            ci.quantity = quantity
            ci.cart_id = _cart_id(request)
            if request.user.is_authenticated:
                ci.user_id = request.user
            ci.save()
            added = True

    return available_qty, cart_quantity, added
Exemplo n.º 7
0
Arquivo: cart.py Projeto: deusesx/VDom
def add_to_cart(request):
    postdata = request.POST.copy()
    product_slug = postdata.get('product_slug', '')
    quantity = postdata.get('quantity', 1)
    p = get_object_or_404(Product, slug=product_slug)
    cart_products = get_cart_items(request)
    product_in_cart = False
    for cart_item in cart_products:
        if cart_item.product.id == p.id:
            cart_item.augment_quantity(quantity)
            product_in_cart = True
    if not product_in_cart:
        ci = CartItem()
        ci.product = p
        ci.quantity = quantity
        ci.cart_id = _cart_id(request)
        ci.save()
Exemplo n.º 8
0
def add_to_cart(request):
    postdata = request.POST.copy()
    product_slug = postdata.get('product_slug', '')
    quantity = postdata.get('quantity', 1)
    p = get_object_or_404(Product, slug=product_slug)
    cart_products = get_cart_items(request)
    product_in_cart = False
    for cart_item in cart_products:
        if cart_item.product.id == p.id:
            cart_item.augment_quantity(quantity)
            product_in_cart = True
    if not product_in_cart:
        ci = CartItem()
        ci.product = p
        ci.quantity = quantity
        ci.cart_id = _cart_id(request)
        ci.save()
Exemplo n.º 9
0
def add_to_cart(request):
    postdata = request.POST.copy()
    # product_slug = postdata.get('product_slug', '')
    quantity = postdata.get('quantity', 1)
    id = postdata.get('product_id')
    # p = get_object_or_404(Product, slug = product_slug)
    p = get_object_or_404(Product, id=id)
    cart_products = get_cart_items(request)
    product_in_cart = False
    for cart_item in cart_products:
        if cart_item.product.id == p.id:
            cart_item.augment_quantity(quantity)
            product_in_cart=True
            return JsonResponse({'product in cart':product_in_cart})
    if not product_in_cart:
        ci = CartItem()
        ci.product = p
        ci.quantity = quantity
        ci.cart_id = _cart_id(request)
        ci.save()
        return JsonResponse({'product in cart': product_in_cart, 'cart_id': ci.cart_id})