Published on

Vendor Order Management using Django in E-commerce Website - EP 29

Introduction

Welcome back to another exciting episode of our E-commerce project series using Django! In this episode, we dive into the intricacies of vendor order management, exploring how to create, view, update, and manage orders and their statuses effectively. Let's get started!

Setting Up: Order List View

The first step is to create views that facilitate the management of orders for vendors. We start by defining a view called orders, which fetches all paid orders associated with the logged-in vendor. We can do this using the following code:

@login_required
def orders(request):
    orders = StoreModel.Orders.objects.filter(vendor=request.user, payment_status='paid')
    context = ('orders': orders)
    return render(request, 'vendor/orders.html', context)

Next, we need to create a template that lists these orders. We can copy an existing template and adjust it to suit our needs for displaying orders.

Order Detail View

The next view we create is for order details. This view retrieves a specific order based on an ID passed as a parameter:

@login_required
def order_detail(request, order_id):
    order = StoreModel.Orders.objects.get(vendor=request.user, id=order_id, payment_status='paid')
    context = ('order': order)
    return render(request, 'vendor/order_detail.html', context)

Having obtained the order details, we set up the corresponding template to render this data.

Order Item Detail View

For managing individual items within an order, we create a view for order item details:

@login_required
def order_item_detail(request, order_id, item_id):
    order = StoreModel.Orders.objects.get(id=order_id)
    item = StoreModel.OrderItem.objects.get(order=order, id=item_id)
    context = ('order': order, 'item': item)
    return render(request, 'vendor/order_item_detail.html', context)

This view enables us to view the details of specific items associated with a particular order.

Updating Order Status

To allow vendors to update the status of orders, we define a function called update_order_status. This updates the order status based on user input received from the front end:

@login_required
def update_order_status(request, order_id):
    order = StoreModel.Orders.objects.get(vendor=request.user, id=order_id, payment_status='paid')
    if request.method == 'POST':
        order.status = request.POST.get('status')
        order.save()
        messages.success(request, 'Order status updated successfully.')
        return redirect('vendor_order_detail', order_id=order.id)
    return redirect('vendor_order_detail', order_id=order.id)

This ensures that whenever a vendor updates the status, they receive feedback and are redirected correctly.

Updating Order Item Status

Similarly, for order items, we create an update function that handles item-specific statuses:

@login_required
def update_order_item_status(request, order_id, item_id):
    item = StoreModel.OrderItem.objects.get(order__vendor=request.user, id=item_id)
    if request.method == 'POST':
        item.status = request.POST.get('status')
        item.shipping_service = request.POST.get('shipping_service')
        item.tracking_id = request.POST.get('tracking_id')
        item.save()
        messages.success(request, 'Item status updated successfully.')
        return redirect('vendor_order_item_detail', order_id=order_id, item_id=item.id)
    return redirect('vendor_order_item_detail', order_id=order_id, item_id=item.id)

URL Configuration

After creating these views, we configure the URLs to correspond with each of the views created. This is essential for ensuring smooth navigation across the application.

Template Updates

Once the URLs are set, we explore updating the templates to reflect the data dynamically, especially in the order and order details templates. We ensure to loop through and display each order and its items properly, thereby providing an intuitive user experience for vendors.

We also ensure to implement CSRF tokens for all forms and handling all future updates to maintain security.

Final Touches

As we finalize the order management system, we conduct thorough testing to ensure that all changes function as expected. The system enables vendors to manage their orders effectively, updating shipping statuses and viewing detailed order information seamlessly.

In the next episode, we will start working with coupons and everything that entails their creation and management!


Introduction

Vendor management, Django, e-commerce, order management, order detail, update status, multi-vendor platform, CSRF token


Introduction

Q1: What is the purpose of the order management feature?
A1: The order management feature allows vendors to view, update, and manage their customers' orders effectively.

Q2: How do I update the status of an order?
A2: Vendors can update the order status by submitting a form that sends a new status to the back end, which processes the change.

Q3: What templates are involved in the order management process?
A3: The templates involved include orders.html for listing all orders, order_detail.html for detailed order information, and order_item_detail.html for specific item details within an order.

Q4: Is CSRF protection implemented in the forms?
A4: Yes, all forms in the order management feature include CSRF tokens to ensure security against cross-site request forgery attacks.

Q5: When can we expect the next episode of this series?
A5: The next episode will focus on coupons and is expected to drop soon—stay tuned!