Skip Navigation

[Résolu] Combined hook between forms and WC

This support ticket is created Il y a 3 années et 5 mois. 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.

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
- 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 7 réponses, has 2 voix.

Last updated by Pat Il y a 3 années et 5 mois.

Assisted by: Minesh.

Auteur
Publications
#1799089

Pat

Hello,

I'm coming back on this ticket : https://toolset.com/forums/topic/hook-for-adding-a-custom-field-in-wc-email/

First of all, I'm not sure the cred_save_data is the right hook to use as this should be done when the order is moving to "In Progress". So, I'm using now the cred_commerce_after_order_completed hook (as the order could be validated manually in the backend, I cannot use the cred_commerce_after_payment_completed). On the WC side, I have seen that 2 hooks could be used : woocommerce_email_order_meta_fields and woocommerce_email_order_meta_keys. What is the difference between these 2 hooks and what is the best one for my current use?

Second, I need to retrieve a custom field which is not in the order, but on the post that have been created thanks to the form. So, I have added the get_post_meta inside to retrieve the $nomevent. Now, how I can pass it to the WC hook? Is it sufficient to put the variable in the 'value' area?

Third, when using this code, I 'm getting a critical error. Could it be that the priorities of each hook have to be done differently ... or is there something else to take into acount?

Last, using the woocommerce_email_order_meta_fields hook, where is displayed the meta fields in the mail?
Here is my code :

function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
    $fields['meta_key'] = array(
        'label' => __( 'Sujet : ' ),
        'value' => $nomevent,
    );
    return $fields;
}
 
add_action('cred_commerce_after_order_completed','func_set_featured_product',10,2);
function func_set_featured_product($post_id,$form_data) {
      
if ($form_data['id']==755) {
           $nomevent = get_post_meta( $nom_id, 'wpcf-nom-de-l-evenement', true );
            add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );
  
    }
}

Regards
Pat

#1800207

Minesh
Supporter

Languages: Anglais (English )

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

Hello. Thank you for contacting the Toolset support.

On the WC side, I have seen that 2 hooks could be used : woocommerce_email_order_meta_fields and woocommerce_email_order_meta_keys. What is the difference between these 2 hooks and what is the best one for my current use?
==>
I suggest you contacting WooCommerce support or WordPress support as those hooks are belongs to WooCommerce not Toolset.

The request you made here is more looks like belongs to WooCommerce rather Toolset.

Please note that the hook you "cred_commerce_after_order_completed" meant to use will be fired from frontend using Toolset form not when you change the order status from the backend.
=> https://toolset.com/documentation/programmer-reference/cred-commerce-api/#cred_commerce_after_order_completed

It seems to me that you will require complete solution using WooCommerce hooks so I strongly suggest that you should contact WooCommerce support first and if they ask how about how Toolset hooks works we are happy to help there.

#1800295

Pat

Hi Minesh,

Thanks for your feedback.
I can understand that the WC hooks are not your concern (but as Toolset wants to integrate fully with WC, I continue to think that there is a deep link between Toolset and WC !).
Second, My first concern here is to pass a custom field from Toolset inside a WC hook. So, again, this is for me 100% part of the Toolset area as I cannot understand by myself how to manage the Toolset hook in combination with the WC hooks !!!

Last point, If I understand well, the cred_commerce_after_order_completed hook will work only if the payment is done through the site and will not fire if I validate manually the order. Is there a way to make some tests without having to buy the product ?

Thanks again for helping.
Regards
Pat

#1800369

Minesh
Supporter

Languages: Anglais (English )

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

The thing is that - you can not declare the variable in one action and use it in another until and unless you define the variable as global variable.

What if you try to use:

function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
 global $nomevent;

    $fields['meta_key'] = array(
        'label' => __( 'Sujet : ' ),
        'value' => $nomevent,
    );
    return $fields;
}
  
add_action('cred_commerce_after_order_completed','func_set_featured_product',10,2);
function func_set_featured_product($post_id,$form_data) {
 global $nomevent;       
if ($form_data['id']==755) {
           $nomevent = get_post_meta( $nom_id, 'wpcf-nom-de-l-evenement', true );
            add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );
   
    }
}

Also, the hooks priority is important here as both hooks are set to run with priority 10. The help you are asking is indeed a custom code which is beyond the scope of our support policy.

That is why I asked you to check first with WooCommerce support about feasibility that if you can use the such hooks the way you are using currently.

#1800379

Pat

Hi Minesh,

Thanks for your help. I will check with WC support team if it's possible to mix different hooks like this.
The fact to add the global variable has improved but there is still an issue (critical error after the order have been paid).
Regards
Pat

#1800497

Pat

Hi Minesh,
Just looking at the WC ezrror log and here is what I found :
2020-10-05T14:01:22+00:00 CRITICAL Uncaught ArgumentCountError: Too few arguments to function func_set_featured_product(), 1 passed in /home/mysite/www/new/wp-includes/class-wp-hook.php on line 287 and exactly 2 expected in /home/mysite/www/new/wp-content/toolset-customizations/ajout-titre-conference-mail.php:19
Stack trace:
#0 /home/mysite/www/new/wp-includes/class-wp-hook.php(287): func_set_featured_product(Array)
#1 /home/mysite/www/new/wp-includes/class-wp-hook.php(311): WP_Hook->apply_filters('', Array)
#2 /home/mysite/www/new/wp-includes/plugin.php(478): WP_Hook->do_action(Array)
#3 /home/mysite/www/new/wp-content/plugins/cred-commerce/classes/event/onordercomplete.php(32): do_action('cred_commerce_a...', Array)
#4 /home/mysite/www/new/wp-content/plugins/cred-commerce/classes/Form_Handler.php(223): CRED_Commerce_Event_OnOrderComplete->execute()
#5 /home/mysite/www/new/wp-content/plugins/cred-commerce/classes/Plugin_Base.php(47): CRED_Commerce_Form_Handler->onOrderComplete(Array)
#6 /home/mysite/www/new/wp-content/plugin dans /home/cannesun/www/new/wp-content/toolset-customizations/ajout-titre-conference-mail.php à la ligne 19

Ligne 19 of my hook is the following :
function func_set_featured_product($post_id,$form_data) {

Could it be an issue with the arguments passed in the Toolset function?

Regards
Pat

#1800513

Minesh
Supporter

Languages: Anglais (English )

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

if you can see our Doc for the hook: cred_commerce_after_order_completed
=> https://toolset.com/documentation/programmer-reference/cred-commerce-api/#cred_commerce_after_order_completed

There is only one argument $data should be passed to the function you attached to hook: cred_commerce_after_order_completed

I'm not sure why you added two argument: $post_id,$form_data

#1800527

Pat

Hi Minesh,

My mistake. I move to only one argument and now, no more critical error !
But the custom field has not been added to the mail !!!
So, I will continue with the WC support team on this topic.
Thanks
Pat

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