[Resolved] Set expiration date based on product selected
This thread is resolved. Here is a description of the problem and solution.
Problem:
Set expiration date based on product selected
Solution:
You can use CRED hook "cred_save_data" to update your post expiration date accordingly.
There is a postmeta key "_cred_post_expiration_time" which is used to store the post-expiration date value as timestamp that you need to update according to your requirement.
Tell us what you are trying to do?
I have two products in Woo:
Product One - Expires 45 days
Product Two - Expires 90 days
I would like to use one CRED form using woo-commerce that sets the expiration date based on which product they selected. I am trying to avoid making two CRED form.
Can I hook into the CRED API to accomplish this?
Is there any documentation that you are following?
CRED API
There is a postmeta key _cred_post_expiration_time which is used to store the post-expiration date value as timestamp that you need to update according to your requirement.
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']==9999){
$timestamp = 9999 ;
update_post_meta($post_id, '_cred_post_expiration_time', $timestamp);
}
}
Where:
- Replace 9999 with your original form ID and $timestamp values according to your need and based on product.
Thank you Minesh. This is a good start. I have a few followup questions:
1- is $timestamp unix time? Am I putting in the number of days I would like for it to expire? ($timestamp = 120;) (120 days)
2- Do I need to get the ID of each product?
1- is $timestamp unix time? Am I putting in the number of days I would like for it to expire? ($timestamp = 120;) (120 days)
==> Yes timestamp should be UNIX timestamp - so you need to calculate it as per number of days from now or what so ever logic you should apply
2- Do I need to get the ID of each product?
==> I assume - are you using Toolset Forms commerce? If yes: there is no way to add multiple products with Toolset form as each Form is associated with unique product.
You can use above hook as per your requirement and needs.
To manually add additional products using WooCommerce's own functions.
You can add a product to the cart with the following:
global $woocommerce;
$woocommerce->cart->add_to_cart($product_id);
There may be another alternatives but you would need to consult the WooCommerce documentation.
And Yes, to check you should use the if..else to check the product ID and based on the product Id you should setup your different post expiration setting using cred_save_data hook.
Hello Minesh. I have the expiration time issue resolved. I am having issues retrieving the product ID.
My code:
// Adjsuts Expiratioin Dates For Job Posts
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']==14652){
global $woocommerce;
$product_id = $product->get_id();
if($product_id == 15052){
$timestamp = current_time('timestamp') + 7 * DAY_IN_SECONDS;
}else{
$timestamp = current_time('timestamp') + 145 * DAY_IN_SECONDS;
}
update_post_meta($post_id, '_cred_post_expiration_time', $timestamp);
}
}
As you can see, I would like if product ID is 15052, I would like to set a specific time stamp, else, set another.
$product_id = $product->get_id(); is throwing up a PHP error.
Well - I need to check how you setup the products and CRED form.
Could you please send me problem URL and access details.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).
I have set the next reply to private which means only you and I have access to it.
Well - I see you changed a bit code with functions.php file for cred_save_data hook.
Are you able to get the product ID - I see the product is setup as the radio buttons and you will be able to get the selected radio button value when you submit the form and value of that selected ratio button will be your selected product ID.
If you still not able to get it - the FTP/SFTP access details you send to me is not working as this end. Please provide me working access details and I would be happy to help.
I have set the next reply to private which means only you and I have access to it.
Hi Minesh, I managed to make this work with your help.
Here is the working code:
// Adjusts Expiratioin Dates For Posts
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']==14652){
//get the value from the field as it appears in the post with the ID from above and assign it to a variable
$product_id = get_post_meta($post_id, 'wpcf-job-post-length', true);
if($product_id == 15052){
$timestamp = current_time('timestamp') + 7 * DAY_IN_SECONDS;
}else if($product_id == 15173){
$timestamp = current_time('timestamp') + 145 * DAY_IN_SECONDS;
}
update_post_meta($post_id, '_cred_post_expiration_time', $timestamp);
}
}
I need help with a new issue, as I cannot get the post to go to published status after payment even though I have it selected. I will open a new ticket, and if you dont mind, I will select you as my point person.