I am trying to:
I've created a post form to edit a cpt, and I'm using the 'cred_before_save_data' hook to update several values for related posts prior to the update taking place. The hook does not appear to be firing as those values are not updating when the form is submitted.
Link to a page where the issue can be seen:
hidden link
Click on any of the listed containers and update them, then check the below inventory screen for the output.
hidden link
I expected to see:
The purpose of the edit screen is to allow a user to edit a container's totes or bags values. Those values are used to calculate the tonage, and value of the container and aggregate them per warehouse and product type as seen in the inventory page. To that end, the ylstracking_edit_container function should remove the original values for totes, bags, tons, and value from the warehouse and product aggregates.
Instead, I got:
The function does not appear to fire at all, instead of subtracting the totes, bags, tons, and value of the pre-updated container, the new values are added on top of the aggregates that were already there.
I understand that i may not be explaining this clearly, so I'll try to outline an example. There is currently a container listed on the edit-confirmed-container page named 'tclu4876263'. This container currently has 15 totes and 15 bags, and if I edit the container to have 20 totes and 20 bags the net result should be an inventory increase of 5 totes and 5 bags. However, since the ylstracking_edit_container function is not working, the actual result is an addition of 20 totes and 20 bags, which is 15 more of each than it should be.
Here is the function I wrote that is not firing. Note I set the priority to 9 so that the aggregation function which I mentioned above fires after this one.
function ylstracking_edit_container($form_data) {
if ($form_data['id']==385) {
if ($_POST['wpcf-product-type']==1) {
$post_id = $form_data['container_id'];
$warehouse = toolset_get_related_post( $post_id, 'warehouse-transaction' );
//this section grabs the original container values for totes, subtracts it from the warehouse and product posts
$container_totes = get_post_meta( $post_id, 'wpcf-container-totes', true);
$warehouse_totes = get_post_meta($warehouse, 'wpcf-warehouse-dt80-totes', true);
$new_warehouse_totes = $warehouse_totes - $container_totes;
update_post_meta( $warehouse, 'wpcf-warehouse-dt80-totes', $new_warehouse_totes );
$inventory_totes = get_post_meta( 75, 'wpcf-inventory-totes', true);
$new_inventory_totes = $inventory_totes - $container_totes;
update_post_meta( 75, 'wpcf-inventory-totes', $new_inventory_totes );
//bags
$container_bags = get_post_meta( $post_id, 'wpcf-container-bags', true);
$warehouse_bags = get_post_meta($warehouse, 'wpcf-warehouse-dt80-bags', true);
$new_warehouse_bags = $warehouse_bags - $container_bags;
update_post_meta( $warehouse, 'wpcf-warehouse-dt80-bags', $new_warehouse_bags );
$inventory_bags = get_post_meta( 75, 'wpcf-inventory-bags', true);
$new_inventory_bags = $inventory_bags - $container_bags;
update_post_meta( 75, 'wpcf-inventory-bags', $new_inventory_bags );
//tons
$container_tons = get_post_meta( $post_id, 'wpcf-container-tons', true);
$warehouse_tons = get_post_meta($warehouse, 'wpcf-warehouse-tons', true);
$new_warehouse_tons = $warehouse_tons - $container_tons;
update_post_meta( $warehouse, 'wpcf-warehouse-tons', round($new_warehouse_tons, 3) );
$inventory_tons = get_post_meta( 75, 'wpcf-inventory-tons', true);
$new_inventory_tons = $inventory_tons - $container_tons;
update_post_meta( 75, 'wpcf-inventory-tons', round($new_inventory_tons, 3) );
//values
$container_value = get_post_meta( $post_id, 'wpcf-container-value', true);
$warehouse_value = get_post_meta($warehouse, 'wpcf-warehouse-value', true);
$new_warehouse_value = $warehouse_value - $container_value;
update_post_meta( $warehouse, 'wpcf-warehouse-value', round($new_warehouse_value, 3) );
}
}
}
add_action('cred_before_save_data', 'ylstracking_edit_container', 9, 1);
Please note you have permission to review and make changes to whatever you need to test the issue.