Page 1 of 1
Problem with after insert hook
Posted: 2014-08-02 12:35
by vikeh
Hi,
I'm having a problem with a simple multiplication.
I have a table invoice_items. When I select the item which is a lookup from another table, the price automatically will be shown as it is an autofill.
After I save it suppose to calculate the Item_Price * Quantity which is not working due to the Price field is an autofill. When I remove the autofill option of the Price field and input it manually, the calculation works.
How can I make it work while the price field is autofill please?
Thanks in advance.
Re: Problem with after insert hook
Posted: 2014-08-14 04:08
by udayvatturi
There are many ways you can achieve this.
As per my knowledge, If the field is auto fill then you will not get that value in $data variable.
1. In hooks you need to write sqlValue to get price from other table using productId (or what ever it is) and then do Item_Price * Quantity
2. Remove autofill,
Do the way you did.
Use magic hooks to make the field readonly,
Re: Problem with after insert hook
Posted: 2016-02-02 04:49
by proque_cu
Hi guys,
I'm quite green to the product....

Are you able to share how do you get around doing this?
I have an orders table in which I want to calculate the price and for more than I read about the hooks not too sure where to start from.
- do I just add the field to the table and head over to the hooks folder?
- or... is there something that I may need to do over at the table side of things before creating a hook...
At the moment I cannot see my "Price" field within the orders.php file.
Thanks,
Re: Problem with after insert hook
Posted: 2016-02-02 23:46
by grimblefritz
Add the field to the table and make it read-only (but don't make it required!)
In the hooks folder, find the 'tablename.php' file and edit it.
In that file, find the 'tablename_after_insert' function and add the php code to calculate and update your price field.
Find the 'tablename_after_update' function and add the same code (or call the 'tablename_after_insert' function and pass along the function arguments.
Or, what I normally do, is add a 'tablename_update_blah" function, and then call that from the other two (insert/update) functions.
The update function would be something like this:
Code: Select all
function tablename_update_fieldname($data) {
$id = $data['id'];
$price = $data['cost'] * $data['qty'];
sql("update tablename set price='{$price}' where id={$id}", $eo);
}
Re: Problem with after insert hook
Posted: 2016-02-03 02:25
by proque_cu
Awesome, thank you very much!!!
