Skip Navigation

[Resolved] CRED+WC: custom field is updated before payment

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 12 replies, has 3 voices.

Last updated by antonK-2 1 year, 4 months ago.

Assisted by: Luo Yang.

Author
Posts
#2563187
Снимок экрана 2023-02-28 в 23.20.52.png
Снимок экрана 2023-02-28 в 23.21.04.png
Снимок экрана 2023-02-28 в 23.21.10.png

I am trying to:
CRED form uses Woocommerce.

add_action('cred_save_data', 'plus_diu_online_kurs',10,2);
function plus_diu_online_kurs ($post_id, $form_data)
{
// Execute for form ID: 123
if ($form_data['id'] == 13882 ) {
// Get currently logged in User ID

// Custom field name
$meta_key = "wpcf-uu";
$dmeta_key = "wpcf-dateuu";

// Fetch the current value of $meta_key for $user_id
$currentValue = get_post_meta($post_id, $meta_key, true);
$dcurrentValue = get_post_meta($post_id, $dmeta_key, true);

// And plus 1 to the $currentValue
$newValue = $currentValue + 1;
$dnewValue = strtotime('Today');
// $dnewValue = date('m.d.y H:i:s');

// Finally, update $meta_key for $user_id with $newValue
update_post_meta( $post_id, $meta_key, $newValue);
update_post_meta( $post_id, $dmeta_key, $dnewValue);

}
}

I expected to see:
Custom field should be updated after payment.

Instead, I got:
Custom field is updated right after moving to checkout page before payment.

Attaching screenshots of the form. Please advise what is wrong?

Thank you in advance
Anton

#2563651

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi there

I think you would want to use one of the hooks specifically for using Toolset Forms Commerce to buy products, see the API details here: https://toolset.com/documentation/programmer-reference/cred-commerce-api/

#2563881
Снимок экрана 2023-03-01 в 16.57.21-3.png

thank you. I've replaced hook but there are error during payment on the page.

add_action('cred_commerce_after_order_completed', 'plus_diu_online_kurs',10,2);
function plus_diu_online_kurs ($post_id, $form_data)
{
// Execute for form ID: 123
if ($form_data['id'] == 13882 ) {
// Get currently logged in User ID

// Custom field name
$meta_key = "wpcf-uu";
$dmeta_key = "wpcf-dateuu";

// Fetch the current value of $meta_key for $user_id
$currentValue = get_post_meta($post_id, $meta_key, true);
$dcurrentValue = get_post_meta($post_id, $dmeta_key, true);

// And plus 1 to the $currentValue
$newValue = $currentValue + 1;
$dnewValue = strtotime('Today');
// $dnewValue = date('m.d.y H:i:s');

// Finally, update $meta_key for $user_id with $newValue
update_post_meta( $post_id, $meta_key, $newValue);
update_post_meta( $post_id, $dmeta_key, $dnewValue);

}
}

#2564493

You will need to follow our document to setup the custom codes:
https://toolset.com/documentation/programmer-reference/cred-commerce-api/#cred_commerce_after_order_completed

For example:

add_action('cred_commerce_after_order_completed', 'plus_diu_online_kurs',10,1);
function plus_diu_online_kurs ($data)
{
	$form_id = $data['extra_data']['cred_form_id'];
	$post_id = $data['extra_data']['cred_post_id'];
...
}
#2564567

I've put it , payment happened but custom field was not updated.

#2565071

Can you copy/paste your new PHP codes here?

#2565367

add_action('cred_commerce_after_order_completed', 'plus_diu_online_kurs',10,1);
function plus_diu_online_kurs ($data)
{

$cred_form_id = $data['extra_data'][0]['cred_form_id'];
$cred_post_id = $data['extra_data'][0]['cred_post_id'];

if($data['extra_data'][0]['cred_form_id'] == 13882 ) {

// Custom field name
$meta_key = "wpcf-uu";
$dmeta_key = "wpcf-dateuu";

// Fetch the current value of $meta_key for $user_id
$currentValue = get_post_meta($post_id, $meta_key, true);
$dcurrentValue = get_post_meta($post_id, $dmeta_key, true);

// And plus 1 to the $currentValue
$newValue = $currentValue + 1;
$dnewValue = strtotime('Today');
// $dnewValue = date('m.d.y H:i:s');

// Finally, update $meta_key for $user_id with $newValue
update_post_meta( $post_id, $meta_key, $newValue);
update_post_meta( $post_id, $dmeta_key, $dnewValue);

}
}

#2566693

Are you going to this: After user complete the payment, update two custom USER field values:
- wpcf-uu
- wpcf-dateuu

Here is a sandbox website:
hidden link

Please try to duplicate your website settings in it, I need a live website to test and debug the custom PHP codes.

#2567417

Please find

1. custom fields: uu and dateuu.
2. post form: updateuu linked to payment
3. post form is placed on "Sample page"
4. woocommerce product: updateuu
5. coupon for 100% discount on payment form: 1

EXPECTED RESULTS
Pressing button of the form fields uu and dateuu should be updated.

REQUIRED ACTION
need to place code below into functions.php as I didn't find how to do it via site:

add_action('cred_commerce_after_order_completed', 'plus_diu_online_kurs',10,1);
function plus_diu_online_kurs ($data)
{

$cred_form_id = $data['extra_data'][0]['cred_form_id'];
$cred_post_id = $data['extra_data'][0]['cred_post_id'];

if($data['extra_data'][0]['cred_form_id'] == 29 ) {

// Custom field name
$meta_key = "wpcf-uu";
$dmeta_key = "wpcf-dateuu";

// Fetch the current value of $meta_key for $user_id
$currentValue = get_post_meta($post_id, $meta_key, true);
$dcurrentValue = get_post_meta($post_id, $dmeta_key, true);

// And plus 1 to the $currentValue
$newValue = $currentValue + 1;
$dnewValue = strtotime('Today');
// $dnewValue = date('m.d.y H:i:s');

// Finally, update $meta_key for $user_id with $newValue
update_post_meta( $post_id, $meta_key, $newValue);
update_post_meta( $post_id, $dmeta_key, $dnewValue);

}
}

#2567433

I assume we are talking about post field group "Anton":
hidden link

I have done below modifications in above test site:
1) Edit product post, setup the price to 0
hidden link
2) Create a page, and display the post form:
hidden link
3) Add one custom code snippet:
hidden link
With below codes:

add_action('cred_commerce_after_order_completed', 'plus_diu_online_kurs',10,1);
function plus_diu_online_kurs ($data){

	$form_id = $data['extra_data'][0]['cred_form_id'];
	$post_id = $data['extra_data'][0]['cred_post_id'];
  
	if($form_id == 29 ) {

		// Custom field name
		$meta_key = "wpcf-uu";
		$dmeta_key = "wpcf-dateuu";

		// Fetch the current value of $meta_key for $user_id
		$currentValue = get_post_meta($post_id, $meta_key, true);
		$dcurrentValue = get_post_meta($post_id, $dmeta_key, true);

		// And plus 1 to the $currentValue
		$newValue = $currentValue + 1;
		$dnewValue = strtotime('Today');
		// $dnewValue = date('m.d.y H:i:s');

		// Finally, update $meta_key for $user_id with $newValue
		update_post_meta( $post_id, $meta_key, $newValue);
		update_post_meta( $post_id, $dmeta_key, $dnewValue);

	}
}

4) Test it in frontend:
hidden link
1) submit the form, and complete the payment
2) Dashboard-> Woocommerce-> Orders:
hidden link
Find the new order, and change the status to: Completed
3) Check the new post:
hidden link
I can see the custom field values are updated correctly.

#2567533

I've changed form type for update and put it into post with name "test".

submission of the form doesn't update fields.

#2568027

I have tried these:
1) Open the "test" post in frontend:
hidden link
Submit the post form

2) Dashboard-> Woocommerce-> Orders:
hidden link
Find the new order, and change the status to: Completed

3) Check the new post:
hidden link
I can see the custom field values are updated correctly.

Can you confirm it first?

#2569283

My issue is resolved now. Thank you!

This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.