在 WooCommerce 电子邮件模板上使用的短代码中传递订单 ID

Pass the Order ID in a shortcode used on WooCommerce Email Templates

我无法通过短代码获取自定义 woocommerce 电子邮件模板中的订单 ID。它确实可以直接工作。

admin-new-order.php 模板中的工作代码:

echo $order->get_id();

我的下面的短代码不起作用,我不知道为什么。我的目标是最终通过短代码将文本变量(如“Hello Firstname”)插入电子邮件模板。

function custom_first_name($order, $order_id){
global $woocommerce, $post;
global $order_id, $order;
$order = new WC_Order( $order_id );
return $order->get_id();
}
add_shortcode( 'custom-woocommerce-name' , 'custom_first_name' );

我通过以下代码将短代码添加到 admin-new-order.php 模板中:

echo do_shortcode('[custom-woocommerce-name]');

$order->get_id() 和短代码都应该输出订单 ID。

要从 admin-new-order.php 模板中使用的短代码中获取 订单 ID,您必须改用以下函数代码:

function wc_custom_first_name( $atts ){
    // Extract shortcode attributes
    extract( shortcode_atts( array(
        'id' => '',
    ), $atts, 'wc-custom-name' ) );

    return $id; // return order id
}
add_shortcode( 'wc-custom-name' , 'wc_custom_first_name' );

代码进入活动子主题(或活动主题)的 functions.php 文件。

然后您将在 admin-new-order.php 模板文件中使用以下内容:

echo do_shortcode("[wc-custom-name id='{$order->get_id()}']");

已测试并有效。

Note:

The available arguments variables passed to admin-new-order.php template are:

  • $order
  • $email_heading
  • $additional_content
  • $sent_to_admin
  • $plain_text
  • $email