Search code examples
djangodjango-oscar

How to Send Email to Django Oscar Registered Partners about Order Placed for there Products?


I want to Notify Django Oscar Partners through an Email stateing that there products has Order from customers. here Customers might place order from multiple Partners for multiple product. how can i send list of order placed for particular Partner and send Email.


Solution

  • Add this Code in Django Oscar Order app at the end of utils.py refer Django Oscar github. in OrderCreator class

    my code is bad optimise/customize it as per requirements.

            if settings.SEND_ORDER_MAILS_ADMIN:
            # Send an email to partner based on order placed
    
            order_line_obj = Line.objects.filter(order=order)
    
            partner_obj_list = []
            for x in order_line_obj:
                if x.partner not in partner_obj_list:
                    partner_obj_list.append(x.partner)
    
            for partner_id in partner_obj_list:
                order_line = Line.objects.filter(order=order,partner=partner_id)
    
                ctx = {'order': order, 'user': order.user, 'order_line': order_line }
                commtype_code = 'PARTNER_ORDER_MAIL' # Add it in CommunicationEventType Model
                try:
                    event_type = CommunicationEventType.objects.get(code=commtype_code)
    
                except Exception as e:
                    messages = CommunicationEventType.objects.get_and_render(code=commtype_code, context=ctx)
                    # print(e)
                else:
                    messages = event_type.get_messages(ctx)
                send_mail(
                    subject="You have an Order # %s"%(order.number),
                    message='Your Message here',
                    html_message=messages['html'],
                    from_email=(settings.EMAIL_HOST_USER, u'Sender Name'),
                    recipient_list=[x.email for x in partner_id.users.all()],
                    fail_silently=False,
                )
        return order # Return