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()
Beispiel #2
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()
Beispiel #3
0
def add_to_cart(request,
                failed_url="/",
                cart_url="/shopping-cart/"):
    """
    Adds an item to cart
    """
    
    if request.method == "POST":
        # form posted, get form info
        data = request.POST.copy()
        quantity = data.get('quantity','')
        product_slug = data.get('product_slug','')
        p = get_object_or_404(Product,slug=product_slug)
        # add item to cart
        try:
            item = CartItem.objects.get(item=p)
        except:
            item = None
        # if this product isnt already in the cart...
        if item is None:
            # create new Cart Item object
            item = CartItem(owner=request.user, item=p, quantity=quantity)
            item.save()
        else:
            # increase the quantity
            item.quantity = item.quantity + int(quantity)
            item.save()
    else:
        # form isnt valid
        return HttpResponseRedirect(failed_url)
    
    # done !
    # redirect to user's cart
    return HttpResponseRedirect(cart_url)
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()
Beispiel #5
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()
Beispiel #6
0
def add_to_cart(request, failed_url="/", cart_url="/shopping-cart/"):
    """
    Adds an item to cart
    """

    if request.method == "POST":
        # form posted, get form info
        data = request.POST.copy()
        quantity = data.get('quantity', '')
        product_slug = data.get('product_slug', '')
        p = get_object_or_404(Product, slug=product_slug)
        # add item to cart
        try:
            item = CartItem.objects.get(item=p)
        except:
            item = None
        # if this product isnt already in the cart...
        if item is None:
            # create new Cart Item object
            item = CartItem(owner=request.user, item=p, quantity=quantity)
            item.save()
        else:
            # increase the quantity
            item.quantity = item.quantity + int(quantity)
            item.save()
    else:
        # form isnt valid
        return HttpResponseRedirect(failed_url)

    # done !
    # redirect to user's cart
    return HttpResponseRedirect(cart_url)
Beispiel #7
0
def create_user():
    for Model in (Role, User, UserRoles, Item, Customer, Cart, CartItem):
        Model.drop_table(fail_silently=True)
        Model.create_table(fail_silently=True)
    user_datastore.create_user(
        email='*****@*****.**',
        password='******'
    )
    item1 = Item(name='notebook', stock=300, price=500)
    item1.save()
    item2 = Item(name='TV', stock=250, price=200)
    item2.save()
    item3 = Item(name='flash', stock=950, price=10)
    item3.save()
    item4 = Item(name='smartphone', stock=455, price=150)
    item4.save()
    item5 = Item(name='camera', stock=50, price=550)
    item5.save()
    customer = Customer(name='John', birthday=date(1990, 1, 15))
    customer.save()
    cart1 = Cart(customer=customer.id)
    cart1.save()
    cartitem = CartItem(cart=cart1, item=item1, quantity=3)
    cartitem.save()
    customer = Customer(name='Olivier', birthday=date(1995, 2, 22))
    customer.save()
    cart2 = Cart(customer=customer.id)
    cart2.save()
    cartitem = CartItem(cart=cart2, item=item5, quantity=45)
    cartitem.save()
Beispiel #8
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()
Beispiel #9
0
def add_to_cart(request, id):
    product = get_object_or_404(Product, pk=id)
    cartItem = CartItem(
        user=request.user,
        product=product,
        quantity=1
    )
    cartItem.save()
    return redirect(reverse('cart'))
Beispiel #10
0
 def add(self, stock_item, unit_price, quantity=1):
     try:
         cart_item = CartItem.objects.get(cart=self.cart, stock_item=stock_item)
         cart_item.quantity = quantity
         cart_item.save()
     except CartItem.DoesNotExist:
         cart_item = CartItem()
         cart_item.cart = self.cart
         cart_item.stock_item = stock_item
         cart_item.unit_price = unit_price
         cart_item.quantity = quantity
         cart_item.save()
Beispiel #11
0
 def add(self, stock_item, unit_price, quantity=1):
     try:
         cart_item = CartItem.objects.get(cart=self.cart,
                                          stock_item=stock_item)
         cart_item.quantity = quantity
         cart_item.save()
     except CartItem.DoesNotExist:
         cart_item = CartItem()
         cart_item.cart = self.cart
         cart_item.stock_item = stock_item
         cart_item.unit_price = unit_price
         cart_item.quantity = quantity
         cart_item.save()
Beispiel #12
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
Beispiel #13
0
def add_to_cart(request, id):
    add_donation = get_object_or_404(donation, pk=id)
    quantity = int(request.POST.get('quantity'))
    try:
        cartItem = CartItem.objects.get(user=request.user,
                                        donation=add_donation)
        cartItem.quantity += quantity
    except CartItem.DoesNotExist:
        cartItem = CartItem(
            user=request.user,
            donation=add_donation,
            quantity=quantity,
        )
    cartItem.save()
    return redirect(reverse('cart_user'))
Beispiel #14
0
def new_cart(request, product_ids=[]):
    """
    Initiate a purchase using the selected product and processing options.
    """    
    assert(len(product_ids))
    CartItem.objects.filter(session_key=request.session.session_key).delete()   
    item_count = 1
    for prodid in product_ids: 
        item = CartItem(
            number = item_count,
            session_key = request.session.session_key,
            product = Product.objects.get(pk=prodid),
            quantity = 1
        )
        item.save()
        item_count += 1
Beispiel #15
0
def add_to_cart(request, id):
    product = get_object_or_404(Product, pk=id)
    quantity = int(request.POST.get('quantity'))

    try:
        cartItem = CartItem.objects.get(user=request.user, product=product)
        cartItem.quantity += quantity
    except CartItem.DoesNotExist:

        cartItem = CartItem(
            user=request.user,
            product=product,
            quantity=quantity
        )
    cartItem.save()
    return redirect(reverse('cart'))
Beispiel #16
0
    def create(cart_id, product_id, quantity):
        """ Create a new cart """
        cart_item = CartItem(cart_id=cart_id,
                             product_id=product_id,
                             quantity=quantity)

        return cart_item.save()
Beispiel #17
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()
Beispiel #18
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()
Beispiel #19
0
def create_cart_item(**kwargs):
    cart_id = kwargs.pop('cart_id', None)

    if not cart_id:
        cart_id = CartItem.generate_cart_id()

    product = kwargs.pop('product', None)

    if not product:
        product = create_product()

    quantity = kwargs.pop('quantity', 1)
    if len(kwargs) > 0:
        raise Exception("Extra keyword args in create_cart_item: %s" % kwargs)

    ci = CartItem(cart_id=cart_id, product=product, quantity=quantity)
    ci.full_clean()
    ci.save()
    return ci
Beispiel #20
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})
Beispiel #21
0
def add(bot, update, args):
    # Function to add to certain Cart # product and quantity
    try:
        if len(args) == 3:
            cart_id = args[0]
            cart = Cart.select().where(Cart.id == cart_id)[0]
            item_name = args[1]
            item = Item.select().where(Item.name == item_name)[0]
            quantity = args[2]
            cart_item = CartItem(cart=cart, item=item, quantity=quantity)
            cart_item.save()
            bot.send_message(
                chat_id=update.message.chat_id,
                text='{}, thanks for shopping with us! '
                'Product: {} in quantity of: {} added to Your Cart {}.'
                '\nIf you need help, please use /guidance'.format(
                    cart.customer, cart_item.item, cart_item.quantity,
                    cart_id))
    except IndexError:
        bot.send_message(chat_id=update.message.chat_id,
                         text='Please use proper format: '
                         '/add Your Cart # Product name Quantity ')