Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14389

Add more than one custom order to a bag session - Django e-commerce application

$
0
0

I am building a Django e-commerce application. I have implemented to ability to leave a custom order on the site and for users to be able to update the quantity, delete the custom order and checkout. I am now trying to refactor the code to allow more than one custom order to be placed in the users bag. What is currently happening is the second custom order is replacing the first custom order in the bag.

context.py file

 if 'custom_order' in bag:        custom_order_id = bag['custom_order']         custom_order = CustomOrder.objects.get(pk=custom_order_id)        if 'quantity' in bag:            total += bag['quantity'] * custom_order.price            individual_total = bag['quantity'] * 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': individual_total,'quantity': bag['quantity'],'product_name': custom_order.product_name,'price': custom_order.price,         })        else:            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,'price': custom_order.price,            })

my view where I am adding to the bag:

def add_custom_order_to_bag(request, custom_order_id):"""Add custom order 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 tried adding a list into the view to handle the multiple custom orders, which seemed like the right way to go, but my context was complaining about receiving a list as the custom_order_id, I wasn't sure how to unpack it so as the context would process the multiple custom_order_id.

EDIT:

so i have added the list to the view and i believe i am unpacking it correctly in a for loop, however when i print out bag_items, only one custom_order is present.

if 'custom_orders' in bag:        custom_order_ids = bag['custom_orders']         for custom_order_id in custom_order_ids:            custom_order = CustomOrder.objects.get(pk=custom_order_id)        if 'quantity' in bag:            total += bag['quantity'] * custom_order.price            individual_total = bag['quantity'] * 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': individual_total,'quantity': bag['quantity'],'product_name': custom_order.product_name,'price': custom_order.price,         })        else:            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,'price': custom_order.price,            })    print(bag_items)
def add_custom_order_to_bag(request, custom_order_id):"""Add custom order to the shopping bag """    custom_order = CustomOrder.objects.get(pk=custom_order_id)    bag = request.session.get('bag', {})    if 'custom_orders' not in bag:        bag['custom_orders'] = []    custom_orders = bag['custom_orders']    custom_orders.append(custom_order_id)    bag['custom_orders'] = custom_orders

Viewing all articles
Browse latest Browse all 14389

Trending Articles