【发布时间】:2018-09-19 05:31:48
【问题描述】:
我正在尝试在 WooCommerce 电子邮件标题中获取订单项目,以获取 emails/email-header.php Woocommerce 模板文件中的一些条件内容
我试过print_r$order,但它是空的,没有给出任何结果。
感谢任何帮助。
【问题讨论】:
标签: php templates woocommerce orders email-notifications
我正在尝试在 WooCommerce 电子邮件标题中获取订单项目,以获取 emails/email-header.php Woocommerce 模板文件中的一些条件内容
我试过print_r$order,但它是空的,没有给出任何结果。
感谢任何帮助。
【问题讨论】:
标签: php templates woocommerce orders email-notifications
获取**$order** 对象的方法是将$email 全局变量重新包含在模板中(就像在相关的挂钩函数中一样):
add_action( 'woocommerce_email_header', 'email_header_before', 1, 2 );
function email_header_before( $email_heading, $email ){
$GLOBALS['email'] = $email;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件中。测试和工作
完成并保存后,您将在模板 emails/email-header.php 的开头插入以下内容:
<?php
// Call the global WC_Email object
global $email;
// Get an instance of the WC_Order object
$order = $email->object;
?>
所以知道您可以在模板上的任何位置使用 WC_Order 对象 $order,例如:
<?php echo __('City: ') . $order->get_billing_city(); // display the billing city ?>
或获取订单商品:
<?php
// Loop through order items
foreach ( $order->get_items() as $item_id => $item ){
// get the product ID
$product_id = $item->get_product_id();
// get the product (an instance of the WC_Product object)
$product = $item->get_product();
}
?>
经过测试并且可以工作
【讨论】: