0

I am working on an ECommerce website, I use signals to update the total value in the 'Order'. -> post_save_cart_total() - is called whenever a new cart is created -> post_save_order() - is called whenever an order is created or changed in the cart -> The cart values are updated using the update_total(), which is called in the post_save_order(), under the if statement(which checks if an order is created or not) -> post_save_order() is called after post_save_cart_total() completes{this fn is called only once/everytime a new cart is created} -> post_save_order() is called twice after the cart creation(post_save_cart_total()) -- 1st time to fill in the values for the order -- 2nd time as an update and is called everytime the order is changed Issue -> The Issue I am facing here is, --When post_save_order() is called for the 1st time, the value of created = True, control goes into the if statement and update total is called to update the values to the order -- The 2nd time when the post_save_order() is called, the value of created is changed created=False and the values are not updated to the order -- So whenever I change the items(add/remove) in the order, the cart values do not change, (i.e) the control does not execute the code under the if statement, as created=False

`

def pre_save_create_order_id(sender, instance, *args, **kwargs):
if not instance.order_id:
    instance.order_id = unique_order_id_generator(instance)
    #instance.save() #--> Since pre_save signal is used here, this is not required
pre_save.connect(pre_save_create_order_id, sender=Order)

def post_save_order(sender, instance, created, *args, **kwargs):
    print('post_save_order')
    print('Before if, created = %s' %created)
    print('Running')
    #instance.update_total()
    if created:
        print('if stmnt, created = %s' %created)
        print('Updating - - - > First')
        instance.update_total()
    else:
        print('else stmnt, created = %s' %created)
        print('2nd entry')
        #instance.update_total()

post_save.connect(post_save_order, sender=Order)

---------> all the signal functions{post_save_cart_total(), post_save_order()} are defined outside the class Order, update_total is defined inside the class Order <-------------

def update_total(self): #This method is called when a cart changes he order size or when an order is created 
    cart_total = self.cart.total
    print(cart_total)
    shipping_total = self.shipping_total
    new_total = cart_total + shipping_total
    print(new_total)
    self.total = new_total
    self.save() 
    return new_total

`

Windows cmd Log:

[27/Sep/2018 14:16:22] "GET /admin/jsi18n/ HTTP/1.1" 200 3189
post_save_order
Before if, created = True
Running
if stmnt, created = True
Updating - - - > First
30.00
33.99
post_save_order
Before if, created = False
Running
else stmnt, created = False
2nd entry 
  • Why don't you use `pre_save` to update the total? – Willem Van Onsem Sep 27 '18 at 19:01
  • Yes, but I need to update the cart order values, (after) everytime the order is changed, so post_save signal is what that I can use to accomplish my task over here. – Guhaprasaanth Sep 27 '18 at 19:06
  • @GuhaPraaanth: but `pre_save` will also trigger that. – Willem Van Onsem Sep 27 '18 at 19:08
  • @WillemVanOnsem : Yes, I've tried using **pre_save_cart_total()** to update the order, every time the cart is changed, by bringing in the **self.save()** from **update_total()** to **pre_save_cart_total()**, but I couldn't seem to do that right – Guhaprasaanth Sep 27 '18 at 19:36

0 Answers0