I am using django sessions for cart Class, now i want to set/store cart_id in session and want to retrieve cart_id whenever it required for passing through url to other views
class Cart(object):
def __init__(self, request, cart_id=None):
self.session = request.session
self.cart_id = cart_id or 1 # Add a cart_id to identify different carts
self.cart_key = f'cart_{self.cart_id}' # Use cart_id to create a unique cart key
self.cart = self.session.get(self.cart_key, {})
if not self.cart:
self.cart = self.session[self.cart_key] = {}
def add(self, product, quantity, update_quantity):
product_id = str(product.id)
if product_id not in self.cart:
self.cart[product_id] = {'quantity': 1.0, 'price': str(product.price), 'tax': str(product.tax)}
else:
self.cart[product_id]['quantity'] = str(float(self.cart[product_id]['quantity']) + quantity)
self.save()
def save(self):
self.session[self.cart_key] = self.cart
self.session.modified = True