Skip Navigation

[Resolved] ‘cred_before_save_data’ hiccup when editing parent relationship

This thread is resolved. Here is a description of the problem and solution.

Problem:

Us action hook cred_before_save_data to do some custom PHP function.

Solution:

It is a custom codes problem, please check the solution here:

https://toolset.com/forums/topic/cred_before_save_data-hiccup-when-editing-parent-relationship/#post-1226957

Relevant Documentation:

This support ticket is created 5 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 6 replies, has 3 voices.

Last updated by MattI4840 5 years ago.

Assisted by: Luo Yang.

Author
Posts
#1225473

I have created an inventory tracking app, with an edit-post form that allows for users to edit out-going shipments. Part of the form is a drop down selector for the parent cpt which keeps a tally of the total inventory in the container. I set up a one to many relationship between Inbound Transactions (inventory), and outbound transactions (parcels) the slug is 'transaction-parcel'.

I also created function called 'ylstracking_edit_parcel' that takes the outbound shipment, and adds it back into inventory using 'cred_before_save_data' before processing the edit-post form. This way the app has an accurate inventory count before validating the new values in the form and processing the new shipment information.

Everything works without issue, unless you change the parent value to different Inbound Transaction. If it is changed, the inventory is never added back to the original Inbound Transaction thus throwing the numbers off.

This is a fairly complicated issue due to the multiple screens and how the inventory is shown on the front end as well as the hamhanded coding I've put together. So I created a duplicator package and that has a very small amount of sample data and provided it here.

Link to a page where the issue can be seen:

To see the issue you should first open the inventory by PO page (Dashboard->Product Inventory->Inventory By PO) to view the two inbound transactions (Test001 and Test002).

Use the edit shipment page (Outbound Workflow->Edit Shipment) to modify the test shipment. If you simply modify the numbers for the shipment (say from 5 to 2), then refresh the Inventory By PO page, you will see that it edits correctly. But if you change the "Darby PO" field (aka the Inbound Transaction) the inventory numbers do not update at all.

I'm understand that this may not be easy to walk through with this info alone. If needed we can schedule a chat or screenshare so I can walk through with you.

#1225568

Hello,

Thanks for the details, I am downloading the files, will update here if there is anything found

#1225576
inventory-by-po.JPG

I can install your duplicator package in my localhost.

I assume we are talking about the problem page:
hidden link

and post form "Edit Outbound Shipment":
hidden link

I have tried it, but there isn't similar problem as you mentioned above:
But if you change the "Darby PO" field (aka the Inbound Transaction) the inventory numbers do not update at all.

Here are detail steps. please correct me if there is anything missing:
1) Login as an administrator user
2) Open URL
hidden link

in the edit form:
- field "Select Darby PO" choose option "TEST002"
- there isn't any field with name "numbers for the shipment" as you mentioned above, so I have tried to change field "Totes" value change to 2
submit the form

3) open URL hidden link

There isn't any text "inventory numbers", see screenshot: inventory-by-po.JPG

please provide detail steps to duplicate the same problem, thanks

#1225770
screenshot006.JPG
screenshot005.JPG
screenshot004.JPG
screenshot003.JPG
screenshot002.JPG
screenshot001.JPG

Lou,

Apologies for not describing the issue very clearly. I'm going to walk through in screens to show you how the issue takes place.

1 - open the hidden link page and you should see two 'Darby PO', TEST001 and TEST002. In my example Test001 has 8 DT80 totes, and 10 DT80 bags (screenshot001).

2 - open hidden link and the Customer PO of JDH001 shows 2 totes of DT80 coming out of the Darby PO TEST001 (screenshot002).

3 - Click JDH001 to enter the edit form hidden link, and change the Darby PO field from TEST001 to TEST002 (screenshot003) and submit the edit.

4 - Go back to or refresh the hidden link page and you'll see that TEST001 still has 8 DT80 totes, and TEST002 still has 10 DT80 totes (screenshot004).

This is the issue, the edit made in step 3 should have added two totes of dt80 back to TEST001 and then subtracted 2 totes of DT80 from TEST002 due to the code in function 'ylstracking_edit_parcel'.

To make this a little more confusing, if you don't change the Darby PO at step 3, and instead just adjust the number of totes or bags the logic holds true and updates correctly. For instance, if I edit JDH001 to have 0 totes but increase bags to 2 (screenshot005), the function places 2 totes of DT80 back into TEST001, and then removes two bags of DT80 from TEST001 (screenshot006).

I hope I was able to articulate that a little better this go around, please let me know what you think may be going on here. I'm going to continue to try to debug on my end. Your help is, as always, greatly appreciated.

#1226328

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Luo has a day off today. He will get in touch with you as soon as he will get back to work on Monday.

#1226957

Thanks for the details, I can see the issue you mentioned above。

I assume we are talking about the custom PHP codes in your theme file functions.php:
line 672 ~ 894:

function ylstracking_edit_parcel($form_data) {
...
}
add_action('cred_before_save_data', 'ylstracking_edit_parcel', 9, 1);

According to our support policy, we don't provide custom codes support:
https://toolset.com/toolset-support-policy/

Here are my suggestions:
1) in your PHP function ylstracking_edit_parcel(), you can get existed related "transaction" post ID like this:

$pre_transaction =  toolset_get_related_post($post_id, 'transaction-parcel');

2) Get the new related "transaction" post ID, like this:

$new_transaction =  $_POST['@transaction-parcel_parent'];

3) Compare above two ID values, if they are different, then do as what you want:
places 2 totes of DT80 back into TEST001, and then removes two bags of DT80 from TEST001

#1227912

My issue is resolved now, I was able to use the syntax you provided to rework the function. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.