I'm having trouble passing the ID of a related post to another custom field using CRED SAVE.
The value getting pushed every time is '0', so i'm assuming the $stream_product_id line below isn't setup correctly.
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==1386)
{
$product_id = get_post( get_the_ID() );
$stream_product_id = toolset_get_related_post( $product_id, 'product-stream', 'parent' );
update_post_meta($post_id, 'wc_pay_per_post_product_ids', $stream_product_id, true);
}
}
Thanks so much for your help in advance!
Hello and thank you for contacting the Toolset support.
I think that get_the_ID() is not returning the correct post ID. It may return the page ID, if the form is used on a page.
I'll suggest using the $post_id that Toolset passes to your function in the first argument:
$product_id = get_post( $post_id );
$stream_product_id = toolset_get_related_post( $product_id, 'product-stream', 'parent' );
Or just:
$stream_product_id = toolset_get_related_post( $post_id, 'product-stream', 'parent' );
I hope this helps. Otherwise, I'll need to take a closer look at your site and check that closely.
Unfortunately that didn't work. It's still passing the value '0' to the custom field.
Maybe it's the process that isn't working for what i wanna do. Here's a run-down:
1) CRED Form that creates a Woo Product
goes to another form->
2) CRED Form that creates a Stream (custom post type) and has a field that establishes relationship to Woo Product
3) CRED Save Data should get the post ID of the related Woo Product, then push that value to a custom field of Woo Product named "wc_pay_per_post_product_ids"
For the record, i've replaced the value "wc_pay_per_post_product_ids" in the update_post_meta portion of CRED SAVE DATA with a generic number to see if that worked, and it did. So i'm not sure what the problem is.
Keith
Please note that $post_id is the ID of the post that is being created or edited by the form. So it will be the ID of the Stream post.
You will need to use it in toolset_get_related_post to get the product_id.
And, I understood, that you want to update the post_meta of the product, rather than the Stream post, right?
In that case, "update_post_meta" should use the product_id, instead of the Stream post id.
If you still find trouble implementing this, please allow me temporary access to your website to check this further. Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **
I will need the following details:
- What form is used to create the product, and on what page it is being used?
- What form is used to create the Stream post, and on what page it is being used?
- Where is located your custom code?
- FTP access to be able to recover the website, in case, I was updating the custom code and I crashed the site.
Thank you for the credentials. I confirm they are working to me.
Here what I gathered so far:
- What form is used to create the product, and on what page it is being used?
The form is "Create Stream Product" and used in the page "Add New Stream".
- What form is used to create the Stream post, and on what page it is being used?
The form is "Create Stream Page" and is used in the page "Add New Stream – Step 2"
I will still need to know:
- Where is located your custom code? It is not available in Toolset->Settings->Custom code. And I do not have access to the theme editor.
- FTP access to be able to recover the website, in case, I was updating the custom code and I crashed the site.
Thank you for the FTP access. As I will need to understand better the workflow of your process and where I can check the value of the custom field(probably in the database, using a tool, such as phpMyAdmin or Adminer), I wonder if you could test the following code:
function my_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==1386)
{
// $product_id = get_post( get_the_ID() );
$stream_product_id = toolset_get_related_post( $post_id, 'product-stream', 'parent' );
update_post_meta($post_id, 'wc_pay_per_post_product_ids', $stream_product_id, true);
}
}
That didn't work. It actually didn't push a value at all.
It would be hard for me to debug the code on your current website. For that reason, I created a test site on our platform, please reproduce a minimal example that can be similar to your case and let me debug it.
You can log in to our test site with the following URL hidden link