0

I am adding a shortcode from wordpress to woocommerce email template.

do_shortcode('[sample_test name="additional_gift_msg"]);

Then i am using this to display the value in email. I am able to display value.

function th_email_shortcode_handler( $atts ) {
    if ( ! empty( $atts['name'] ) ) {
        $field = $atts['name'];
        echo 'Found me';
    }
}
add_shortcode('sample_test','th_email_shortcode_handler');

But i need $order or $order_id inside this handler function to take some value from post meta. How can i use those variables? The shortcode handler function is in functions.php

1 Answer 1

1

WooCommerce Email template already has access to $order variable. You simply need to add it to shortcode atts, like this

do_shortcode('[sample_test name="additional_gift_msg" order_id=' . $order->get_id() . ']');

Then in the shortcode callback:

function th_email_shortcode_handler( $atts ) {
    // get the $order_id
    $order_id = $atts['order_id'];

    // create the order object if needed
    $order = new WC_Order( $order_id );

    if ( ! empty( $atts['name'] ) ) {
        $field = $atts['name'];
        echo 'Found me';
    }
}
13
  • still $order_id is null when var_dumped @Manzoor
    – melvin
    Oct 4, 2018 at 4:25
  • How are you trying to add that information to the email template? Using the hook or by placing the template in woocommerce directory in the theme? Oct 4, 2018 at 4:34
  • I have a custom email template and that email template is passed to woocommerce. When i var_dump $order in email template it shows required value. But i am not getting value in function
    – melvin
    Oct 4, 2018 at 4:43
  • Can you please show/explain how you pass the template to WooCommerce? Oct 4, 2018 at 4:51
  • using woocommerce_locate_template hook
    – melvin
    Oct 4, 2018 at 4:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.