Skip Navigation

[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.

You can find proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/set-expiration-date-based-on-product-selected/#post-914118

Relevant Documentation:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created 6 years, 6 months 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 10 replies, has 2 voices.

Last updated by Armando 6 years, 6 months ago.

Assisted by: Minesh.

Author
Posts
#907818

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

Thank you!

#908310

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Yes - its possible using the Toolset forms API (Please note the CRED plugin name is renamed as Toolset Froms now).

You can use CRED hook cred_save_data to update your post expiration date accordingly.
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

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.

#908579

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?

Ex:

$mypost_id = $post_id;
if ($mypost_id == 12345) {
  $timestamp = xxx;
}elseif ($mypost_id == 12333){
  $timestamp = xxx;
}elseif($mypost_id = 33334){
  $timestamp = xxx;
}
update_post_meta($mypost_id, '_cred_post_expiration_time',  $timestamp);

I need to adjust expiration date based on multiple products in one form.

Thanks Minesh.

#908813

Minesh
Supporter

Languages: English (English )

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

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.

The only thing I can suggest to add multiple products would be to use the hook: cred_commerce_add_product_to_cart
=> https://toolset.com/documentation/programmer-reference/cred-commerce-api/#cred_commerce_add_product_to_cart

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.

#911800

Hi Minesh, I would like a few more days on this if you don't mind.

Thanks!

#911803

Minesh
Supporter

Languages: English (English )

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

Yes - sure. Take your time and get in touch with me if you need further assistance.

#913238

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.

Thank you!!!!

#913388

Minesh
Supporter

Languages: English (English )

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

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.

#913520

Minesh
Supporter

Languages: English (English )

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

Well - could you please share link of the page where I can see the form.

I will look at this issue tomorrow first thing.

I have set the next reply to private which means only you and I have access to it.

#913853

Minesh
Supporter

Languages: English (English )

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

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.

#914118

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.

Thanks!