Skip Navigation

[Resolved] Hook for adding a custom field in WC email

This support ticket is created 4 years, 2 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 7 replies, has 2 voices.

Last updated by Minesh 4 years, 2 months ago.

Assisted by: Minesh.

Author
Posts
#1753681

Pat

Hello,

I have a site with Toolset and WC. As it is not possible to manage variable products in front end with Toolset, I'm trying to find a workaround.
Inside the standard WC product, I have inserted a WC forms that enables to chose between different products (here, these products are just used to define the price of the initial product).
No issue until there, but now, I need to insert the title of the initial product inside the WC emails. For this, WC propose a hook :

/**
 * Add a custom field (in an order) to the emails
 */
add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );

function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
    $fields['meta_key'] = array(
        'label' => __( 'Label' ),
        'value' => get_post_meta( $order->id, 'meta_key', true ),
    );
    return $fields;
}

In addition, only some specific products should be managed like this (the single products do not need it and should be treated as a standard products).
My question is : how can I merge the WC hook in the same time than a cred_save_data hook that will be created for the specific Forms that will be used ?

Regards
Pat

#1754845

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Do you mean that you want to run/trigger the hook you added "woocommerce_email_order_meta_fields" when you have variable product? If yes:

As this is a standard WC hook I suggest you should contact WC support as there is no way to merge two hooks that is standard WC hook and Toolset cred_save_data hook.

#1756285

Pat

Hi Minesh,

Thanks for coming back.
Perhaps I did not have been clear : I want that the "cred_save_data" issued by the Forms lauch in the same time the "woocommerce_email_order_meta_fields" function in orde rto add the related fields to the current order.
Hope this clarify the need.
Regards
Pat

#1756351

Minesh
Supporter

Languages: English (English )

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

As I understand - you want to trigger the hook "woocommerce_email_order_meta_fields" from within the"cred_save_data" hook?

#1756423

Pat

Yes Minesh,

As I want to launch this hook only for certain products (defined where the Forms is placed - using conditional display for this).
Regards
Pat

#1757705

Minesh
Supporter

Languages: English (English )

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

Once you setup and configure your hook "woocommerce_email_order_meta_fields" with add_filter().

Within the cred_save_data action, what if you try to call the filter. For example - something like as given under:

function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
    $fields['meta_key'] = array(
        'label' => __( 'Label' ),
        'value' => get_post_meta( $order->id, 'meta_key', true ),
    );
    return $fields;
}

add_action('cred_save_data','func_set_featured_product',10,2);
function func_set_featured_product($post_id,$form_data) {
     
if ($form_data['id']==9999) {
   
            add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );
 
    }
}

Where:
- Replace '9999' with your Toolset form ID

#1758703

Pat

Hi Minesh,

Thanks, I will test this and let you know if any issue.
Regards
Pat

#1759025

Minesh
Supporter

Languages: English (English )

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

Ok - thank you.