Hi, I'm using Toolset to create a inventory tracking system, I'm new to these plugins so what I'm looking for may be basic functionality that I'm just missing. Some quick background, thus far I've created 3 custom post types, Products which holds all info for a specific products including a field for total inventory, transactions which holds all information for incoming and outgoing inventory, and customers which just holds customer info.
My question is how would I go about updating the total inventory field when a new transaction is submitted. Meaning if an inbound (purchase) transaction is submitted, I need to take the number of items that are being purchased and add that numeric value of the "total inventory" field, and the opposite for an outbound (sale) transaction.
Thanks in advance!
Matt
Hi, thanks for trying out Toolset! First, I would like to point out that WooCommerce does shop and inventory management really well, so if you're looking for an eCommerce solution that works out of the box (and is FREE), definitely look into that. Not sure of your level of knowledge of WordPress so I just wanted to throw it out there. You'll save yourself a lot of headache trying to build a shop from scratch. On the other hand if you're just experimenting or need more information about how you can build something custom with Toolset, read on.
My question is how would I go about updating the total inventory field when a new transaction is submitted.
Assuming you use Toolset Forms to create a new "Transaction" post, then you can use the Forms API cred_save_data to automatically update custom field values in the "Product" post based on the quantity of that Product chosen in the Transaction post. You will add some custom PHP code in your child theme's functions.php file. In that code, you can "get" the quantity of each Product in the Transaction, and "set" the inventory values of each Product with the updated values.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
Here's an example of how to get the value of a custom field:
$count = get_post_meta( 12345, 'wpcf-inventory', true);
Change 12345 to match the ID of some Product post, or a variable that represents that ID. Change 'inventory' to match the slug of the inventory count custom field. Leave 'wpcf-' in place as the field slug prefix here in the code.
Here's an example of how to update the value of the corresponding inventory custom field in the Product post after you have calculated the new inventory count, which is now 10 items:
update_post_meta( 67890, 'wpcf-inventory', 10 );
Change 67890 to match the Product post ID, or a variable that represents that ID.
I can help with any code that touches Toolset APIs. Let me know what you need.
Christian,
Thank you for getting back to me with this information, it's extremely helpful. WooCommerce is fantastic for eCommerce, but what I'm working on doesn't marry up well with out of the box solutions.
I'll be testing the api you sent over today and tomorrow and I'll let you know if I hit any roadblocks with implementing it.
Matt
Christian,
I've been experimenting with the info you sent, and I'm having some issues getting the post meta to update. I'm just getting acquainted with toolset, and getting back into php so I'm sure some of this is elementary, but your help is greatly appreciated.
here is the update function I wrote which is located in the themes functions.php file.
function xtracking_update_inventory() {
global $post;
//Check to ensure correct from is being used, 'confirm deliver is form 101'
if ($form_data['id']==101) {
//check for product type in the current form and add the value of total inventory to the product post
//note there are only two product at this point DT80 and DT98
if ($fields['wpcf-product-type']['value']==DT80) {
$count = get_post_meta( $post->ID, 'wpcf-containers', true);
update_post_meta( 82, 'wpcf-total-inventory', $count );
} if ($fields['wpcf-product-type']['value']==DT98) {
$count = get_post_meta( $post->ID, 'wpcf-containers', true);
update_post_meta( 81, 'wpcf-total-inventory', $count );
}
}
}
add_action('cred_save_data', 'xtracking_update_inventory');
Thanks
Okay I see a few things.
1. You're trying to access the post that was just created by using the global $post. The cred_save_data hook will receive two arguments, $post_id and $form_data. You can use the $post_id parameter to access the new post's ID. The global $post won't work here.
2. Your code uses a variable $fields that is undefined, as far as I can tell. If you want to access a value submitted in one of the custom fields, you can use the $_POST superglobal like this:
$field = $_POST['wpcf-field-slug'];
3. DT80 and DT98 are strings, so they should be wrapped in quotation marks.
A few updates below:
function xtracking_update_inventory( $post_id, $form_data ) {
//Check to ensure correct from is being used, 'confirm deliver is form 101'
if ($form_data['id']==101) {
//check for product type in the current form and add the value of total inventory to the product post
//note there are only two product at this point DT80 and DT98
if ($_POST['wpcf-product-type']=='DT80') {
$count = get_post_meta( $post_id, 'wpcf-containers', true);
update_post_meta( 82, 'wpcf-total-inventory', $count );
} if ($_POST['wpcf-product-type']=='DT98') {
$count = get_post_meta( $post_id, 'wpcf-containers', true);
update_post_meta( 81, 'wpcf-total-inventory', $count );
}
}
}
add_action('cred_save_data', 'xtracking_update_inventory', 10, 2);
Hey Christian,
I'm still having issues getting this function working, I've requested a support video call just FYI. I also wanted to provide you with the updated code I have, you'll see some things have changed in terms of id numbers as I've moved to a local testing environment.
function ylstracking_update_inventory($post_id, $form_data) {
//Check to ensure correct from is being used, 'confirm deliver is form 1815'
if ($form_data['id']==1815) {
//check for product type in the current form and add the value of total inventory to the product post. DT80 = 1, DT98 = 2
if ($_POST['wpcf-product-type']==1) {
$count = get_post_meta( $post->ID, 'wpcf-containers', true);
update_post_meta( dt80, 'wpcf-total-inventory', $count );
} if ($_POST['wpcf-product-type']==2) {
$count = get_post_meta( $post->ID, 'wpcf-containers', true);
update_post_meta( dt98, 'wpcf-total-inventory', $count );
}
}
}
add_action('cred_submit_complete', 'ylstracking_update_inventory', 10, 2);
Thanks
update_post_meta( dt80, 'wpcf-total-inventory', $count );
Read the documentation for update_post_meta again. You must use a numeric post ID, not dt80.
update_post_meta( dt98, 'wpcf-total-inventory', $count );
Read the documentation for update_post_meta again. You must use a numeric post ID, not dt98.
if ($_POST['wpcf-product-type']==1)
....
if ($_POST['wpcf-product-type']==2)
Your code implies that the options for the "product-type" custom field are 1 and 2. Is that correct? You can tell by editing the custom field (Toolset > Custom Fields) and checking the options. Take a screenshot of that field's options and provide it in the next reply.
Christian,
Thanks for your patients and your attention to this issue sir. Here is the updated function and screenshot. Now that I've made the below changes, the total-inventory field is being updated, but it is being overwritten with a null value. I'm wondering if the get_post_meta function isn't grabbing the containers field and placing it in the count variable.
function ylstracking_update_inventory($post_id, $form_data) {
//Check to ensure correct from is being used, 'confirm deliver is form 1815'
if ($form_data['id']==1815) {
//check for product type in the current form and add the value of total inventory to the product post. DT80 = 1, DT98 = 2
if ($_POST['wpcf-product-type']==1) {
$count = get_post_meta( $post->ID, 'wpcf-containers', true);
update_post_meta( 1818, 'wpcf-total-inventory', $count );
} if ($_POST['wpcf-product-type']==2) {
$count = get_post_meta( $post->ID, 'wpcf-containers', true);
update_post_meta( 1819, 'wpcf-total-inventory', $count );
}
}
}
add_action('cred_submit_complete', 'ylstracking_update_inventory', 10, 2);
The screenshot you requested is also attached to confirm that dt80 = 1 and dt98 = 2, and since those posts are being updated (albeit with a null) I believe that part to be correct.
Thanks,
Matt
$count = get_post_meta( $post->ID, 'wpcf-containers', true);
Remember $post->ID is not going to work here. You must use $post_id from the callback arguments, so update both lines that use $post->ID to be $post_id instead.
Christian,
Thank you so much for your help, the function is working as expected now and I have a much better understanding of how to structure things for this project. The education and support are very much appreciated!
Thanks,
Matt