获取 Woocommerce 电子邮件标题模板中的订单对象

dan*_*mad 1 php templates orders email-notifications woocommerce

我正在尝试为emails/email-header.phpWoocommerce 模板文件中的某些条件内容获取 WooCommerce 电子邮件标头中的订单项

我曾尝试print_r$order,但它是空的,而不是让任何结果。

任何帮助表示赞赏。

Loi*_*tec 9

获取**$order**对象的方法是将$email全局变量包含在模板中(应该是这样,就像在相关的挂钩函数中一样):

add_action( 'woocommerce_email_header', 'email_header_before', 1, 2 );
function email_header_before( $email_heading, $email ){
    $GLOBALS['email'] = $email;
}
Run Code Online (Sandbox Code Playgroud)

代码位于活动子主题(或活动主题)的 function.php 文件中。测试和工作

一旦完成保存在你的模板电子邮件/电子邮件的header.php要插入开头如下:

<?php
    // Call the global WC_Email object
    global $email; 

    // Get an instance of the WC_Order object
    $order = $email->object; 
?>
Run Code Online (Sandbox Code Playgroud)

所以知道你可以$order在模板上的任何地方使用 WC_Order 对象,例如:

<?php echo __('City: ') . $order->get_billing_city(); // display the billing city ?>
Run Code Online (Sandbox Code Playgroud)

或获取订单项目:

<?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();
    } 
?>
Run Code Online (Sandbox Code Playgroud)

测试和工作