pratyushpundir6424
pratyushpundir6424

I have an Order model that uses a hasMany['lineItems'] relationship. I use the Relation Manager to add LineItems to a new Order. Right now, the Order Total can be calculated only AFTER an Order is saved (since LineItems are only attached to the Order after it has been saved - deferred binding). I want to be able to display OrderTotal while LineItems are being added (and not after Order save) using AJAX so that the user can see how adding LineItems is going to affect the final Total Order Value.

Here are a few screenshots to help understand the situation:

  1. Creating a New Order: http://pasteboard.co/12xeUuBK.png

  2. Adding Line Items: http://pasteboard.co/12xhWe0O.png

  3. Line Items Added (but not attached to this Order till its saved since this uses deferred binding): You can see that the Order Total field is blank. http://pasteboard.co/12xmOYhs.png

  4. Order saved (total can be calculated now since we know which line items are added to this order): http://pasteboard.co/12xu1BYX.png

Any help will be much appreciated. Feel free to ask for any code here.

vanchuck
vanchuck

Not sure if you ever found a solution to this. But whenever an item is added/removed to the relation manager, the entire relationmanager (including toolbar) is refreshed. Because of this you could put some javascript in the toolbar partial (which will therefore run each time, I think?). The JS would do some DOM traversal to find each of the rows in the list. Within each row (eg in the subtotal field) you could create a partial which outputs not only the total but a hidden input or other tag which has the subtotal in numeric form which could be casted to a Float.

Then add up all those numbers for of the rows and update the field value of the Total field using jQuery.

taylankoca
taylankoca

I have the same problem. In some circumstances, there is no access to relation toolbar. It is created automatically. So there no chance to add a javascript code.

alxy
alxy

There is a chance to get the deferred items: https://github.com/responsiv/pay-plugin/blob/master/models/Invoice.php#L158

You just need to refresh the form (or only the total field) in this case.

1-4 of 4