【问题标题】:Get the order object in Woocommerce email-header template获取 Woocommerce 电子邮件标头模板中的订单对象
【发布时间】:2018-09-19 05:31:48
【问题描述】:

我正在尝试在 WooCommerce 电子邮件标题中获取订单项目,以获取 emails/email-header.php Woocommerce 模板文件中的一些条件内容

我试过print_r$order,但它是空的,没有给出任何结果。

感谢任何帮助。

【问题讨论】:

    标签: php templates woocommerce orders email-notifications


    【解决方案1】:

    获取**$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();
        } 
    ?>
    

    经过测试并且可以工作

    【讨论】:

    • 成功了,谢谢兄弟 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-08
    • 1970-01-01
    • 2019-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多