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

标签 php wordpress templates woocommerce shortcode

我无法通过简码获取自定义 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

关于php - 在 WooCommerce 电子邮件模板上使用的短代码中传递订单 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65499203/

相关文章:

php - SQL 查询当前日期在开始/结束日期内的条目

apache - PHP和不存在的文件

mysql - 使用 mySQL/Wordpress 从元数据列获取数据

php - 使用博客文章作为缩略图库

c++ - 我可以将类模板添加到元组吗?

javascript 模板引擎保持对文本框等的关注?

php - MySQL Date_format错误

php - 如何从Windows Live Mail导出电子邮件

c++ - 仅在实例化时使用 `static_assert` 使类模板特化无法编译的安全、符合标准的方法?

php - 带有 XMLHttpRequest 的 open 方法的 javascript : using post and its parameters