Skip Navigation

[Closed] cred_commerce_after_payment_completed – $data finds legacy data to cause error

This support ticket is created 2 years, 8 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
- 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/Karachi (GMT+05:00)

This topic contains 6 replies, has 2 voices.

Last updated by Waqar 2 years, 7 months ago.

Assisted by: Waqar.

Author
Posts
#2307773

I have a function triggered by cred_commerce_after_payment_completed which updates custom post data if $form_id==928, developed about 5 years ago, following support tickets raised by other people!

add_action('cred_commerce_after_payment_completed','xxx_advert_update',10,1);

function xxx_advert_update($data)
{
// Extract relevant elements from data
    $form_id = $data['extra_data'][0]['cred_form_id'];
    $post_id = $data['extra_data'][0]['cred_post_id'];
// if form is the Advert Activation Form, set status to 'live' after payment and change end date
if ($form_id==928)
    {
    $ad_listing_status="Live";
    $ad_status_field="wpcf-xxx-ad-status";
    update_post_meta($post_id, $ad_status_field, $ad_listing_status);   
    $xxx_end_date = time() + (365 * 24 * 60 * 60);  
    $ad_end_field = "wpcf-xxx-ad-end-date";
    update_post_meta($post_id, $ad_end_field, $xxx_end_date);   
    }
}

We have recently identified that the code is being run against apparently random posts when form 928 isn't submitted.
I have been unable to identify any plausible pattern for this until today, and I think this is the sequence of events.
1. A user (not me) submits form 928 and pays. Their post is set up fine.
2. I start working on a post, using various post forms and editing in back-end too.
3. A third user makes a purchase that is NOT connected to a CRED form, but the code above runs on my post data.
My supposition is that (a) $form_id still contains '928' because it was the last cred form submitted for payment; (b) $post_id contains the id of my post, because I have been working on it; (c) the code therefore runs against my post when the new purchase is made.

I cannot imagine any other explanation, because the $xxx_end_date field is ONLY set in this piece of code, and nowhere else. Is this plausible, and if so, how can I avoid it? e.g. should I empty the value of $post_id after running this bit of code?

Any thoughts welcome, thanks!

#2308121

Hi,

Thank you for contacting us and I'd be happy to assist.

What you're experiencing seems expected, as your code only checks for a specific form ID and not for any specific created post ID or involved product ID.

If you could share details about the requirement and background information for this form and the custom code, I'll be in a better position to suggest some changes to this code, to make it more precise.

regards,
Waqar

#2308329

Hi Waqar,
Thanks for getting in touch. Sounds like you think this will be an easy fix. 🙂
Business process is:
1. User creates a draft post using a different form (this is a custom post type).
2. User pays for the post using form 928 (a post edit form configured for commerce).
3. Code updates fields of the associated post that has just been edited by the form.
I guess I just need an extra condition that ensures only the post edited by 928/associated with the payment is updated?
I should probably mention that the product used is dependent on a selection field in form 928, so the code might be triggered by purchasing product 925 or 926.
For info, there are other transactions going on via WooCommerce, unrelated to paying to activate posts, and not associated with Toolset forms.
Let me know if you need more details.
Thanks.
Neil

#2310035

Thank you for sharing these details.

I'm currently performing some tests on my website, with a similar setup and will share my findings, as soon as this testing completes.

Thank you for your patience.

#2311209

Thank you for waiting, while I performed some tests on my website.

During testing, I noticed that the custom function attached to the hook 'cred_commerce_after_payment_completed' is only executed when a user submits the Toolset post form, which is set to add a product to the cart, and then that product's order is placed, by the same user.

Other than that, if that or some other user directly places the order (i.e. without using the Toolset post form first), through the normal checkout process, the custom function attached to the hook 'cred_commerce_after_payment_completed' is not executed.

Based on these tests, the suppositions that you've made seem incorrect. If you'd like to track the execution of this custom function attached to the hook 'cred_commerce_after_payment_completed', you can include some debugging lines, which can write some entries to the error log, every time this code is executed:


add_action('cred_commerce_after_payment_completed','xxx_advert_update',10,1);
 
function xxx_advert_update($data)
{
// Debugging output	
    $t=time();
    error_log(date("Y-m-d h:i:sa",$t));
    error_log('------------------------------------');
    error_log(print_r($data,true));
    error_log('==================================================================================================');

// Extract relevant elements from data
    $form_id = $data['extra_data'][0]['cred_form_id'];
    $post_id = $data['extra_data'][0]['cred_post_id'];
// if form is the Advert Activation Form, set status to 'live' after payment and change end date
if ($form_id==928)
    {
    $ad_listing_status="Live";
    $ad_status_field="wpcf-xxx-ad-status";
    update_post_meta($post_id, $ad_status_field, $ad_listing_status);   
    $xxx_end_date = time() + (365 * 24 * 60 * 60);  
    $ad_end_field = "wpcf-xxx-ad-end-date";
    update_post_meta($post_id, $ad_end_field, $xxx_end_date);   
    }
}

These extra debugging lines will write the current timestamp and the "$data" array's content, whenever this function will be executed.

Note: Please make sure that error logging is enabled on the website and you can include the following lines in the website's "wp-config.php" file, to enable it:
( ref: hidden link )


define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );

I hope this helps and please let me know how it goes.

#2314767

Hi Waqar,
That's very helpful, thanks. I'll certainly give that a go and let you know what I find.
Neil

#2316175

Please take your time and I'll wait to hear back from you.

The topic ‘[Closed] cred_commerce_after_payment_completed – $data finds legacy data to cause error’ is closed to new replies.