如何在简码中访问woocommerce订单变量?

时间:2018-10-04 05:05:17

标签: wordpress shortcode

我正在将wordpress的简码添加到woocommerce电子邮件模板中。

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

然后我正在使用它来显示电子邮件中的值。我能够显示价值。

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

add_shortcode('sample_test','th_email_shortcode_handler');

但是我需要在此处理函数中使用$order$order_id才能从post meta中获取一些价值。如何使用这些变量?简码处理程序函数位于functions.php中。

我也尝试了以下操作,但$ order_id仍然为空。

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

1 个答案:

答案 0 :(得分:0)

下面的代码将解决问题。

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

    global $wp;

    $order_id  = absint( $wp->query_vars['order_id'] );

    if ( empty($order_id) || $order_id == 0 )
        return; // Exit;

    return $order_id;
    }

add_shortcode('sample_test','th_email_shortcode_handler');