Is there a way to temporarily remove items from a session and re-add after code has run

I am working on an e-commerce site, using Django. I am trying to add a custom order to the bag which is happening, however now I cannot add other products to the bag when the custom order is in the bag. I was thinking if I removed the custom_order from the session, it would allow the other code to run, in order to add products to the bag.

context.py

from decimal import Decimal
from django.conf import settings
from django.shortcuts import get_object_or_404
from products.models import Product
from custom_order.models import CustomOrder

def bag_contents(request):

    bag_items = []
    total = 0
    bag = request.session.get('bag', {})

    print(bag)
     
    if 'custom_order' in bag:
        custom_order_id = bag['custom_order'] 
        custom_order = CustomOrder.objects.get(pk=custom_order_id)
        total += custom_order.price
        bag_items.append({
            'product_id': custom_order.product_id,
            'custom_order_id': custom_order_id,
            'category': custom_order.category,
            'material': custom_order.material,
            'individual_total': custom_order.price,
            'quantity': 1,
            'product_name': custom_order.product_name
        })
    else:
        for product_id, quantity in bag.items():
            product = Product.objects.get(pk=product_id)
            if isinstance(quantity, int) and isinstance(product.price, Decimal):
                total += quantity * product.price
                individual_total = quantity * product.price
                product_count += quantity
                bag_items.append({
                'product_id': product_id,
                'quantity': quantity,
                'product': product,
                'individual_total': individual_total,
            })
    
    grand_total = total
    

    context = {
        'bag_items': bag_items,
        'grand_total': grand_total,
    }

    return context

View to add product to bag

def add_to_bag(request, product_id):
    """ Add a quantity of the specified product to the shopping bag """
    quantity = int(request.POST.get('quantity'))
    redirect_url = request.POST.get('redirect_url')
    
    bag = request.session.get('bag', {})

    if product_id in list(bag.keys()):
        bag[product_id] += quantity
    else:
        bag[product_id] = quantity

    request.session['bag'] = bag
    return redirect(redirect_url)

view to add custom_order to bag

def add_custom_order_to_bag(request, custom_order_id):
    """ Add a quantity of the specified product to the shopping bag """
    custom_order = CustomOrder.objects.get(pk=custom_order_id)
    bag = request.session.get('bag', {})

    bag['custom_order'] = custom_order_id
   
    request.session['bag'] = bag
    return redirect(reverse('view_bag'))

I have tried this:

def add_to_bag(request, product_id):
    """ Add a quantity of the specified product to the shopping bag """
    quantity = int(request.POST.get('quantity'))
    redirect_url = request.POST.get('redirect_url')
    
    bag = request.session.get('bag', {})

    custom_order_id = bag.pop('custom_order', None)

    if product_id in list(bag.keys()):
        bag[product_id] += quantity
    else:
        bag[product_id] = quantity

    if custom_order_id:
        bag['custom_order'] = custom_order_id

    request.session['bag'] = bag
    return redirect(redirect_url)

but it is not working, I am getting no errors so think the logic might be off!

Back to Top