【问题标题】:Add the order ID to the order item metadata before the order is saved and WooCommerce email notifications are sent在保存订单并发送 WooCommerce 电子邮件通知之前,将订单 ID 添加到订单项目元数据
【发布时间】:2022-01-04 10:57:35
【问题描述】:

我需要将带有订单 ID 和密码的链接添加到订单商品元数据。

目前,我正在使用 woocommerce_checkout_create_order_line_item 操作成功地将其他信息添加到项目元数据,但是当此操作运行时,订单 ID 还无法访问 (?)。

我可以通过其他方式执行此操作,以便在保存订单并向客户发送通知之前添加链接吗?

add_action( 'woocommerce_checkout_create_order_line_item', 'custom_checkout_create_order_line_item', 20, 4 );
function custom_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
    if( ! isset( $values['test_title'] ) ) { return; }

    // add test info as order item metadata
    if( ! empty( $values['test_title'] ) ) {
        $test_pw = wp_generate_password(); // generate a password
        $item->update_meta_data( '_test_id', $values['test_id'] ); // add test id
        $item->update_meta_data( '_test_password', $test_pw ); // add test access password
        $item->update_meta_data( 'Access link', // add test access permalink
            get_permalink( $values['test_id'] ) . '?order=' . $order->get_id() . '&pw=' . $test_pw );
    }
}

【问题讨论】:

    标签: wordpress woocommerce hook-woocommerce orders email-notifications


    【解决方案1】:

    您的钩子在订单保存之前执行,并且在“保存”步骤中确定订单 ID。

    所以除非你可以在你的钩子中包含这个功能,否则我不相信有办法在那个时候知道订单 ID。

    但是,根据您的问题,我了解到它涉及您希望在电子邮件中显示的项目元数据。那么使用woocommerce_order_item_meta_startwoocommerce_order_item_meta_end 钩子不是一个选项,它允许您在wc_display_item_meta() WooCommerce 函数的输出之前或之后添加其他产品信息:

    function action_woocommerce_order_item_meta_start( $item_id, $item, $order, $plain_text ) {
        // On email notifications
        if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) {
            echo '<ul class="wc-item-meta"><li><strong class="wc-item-meta-label">Label</strong><p>Value order id = ' . $order->get_id() . '</p></li></ul>';
        }
    }
    add_action( 'woocommerce_order_item_meta_start', 'action_woocommerce_order_item_meta_start', 10, 4 );
    

    【讨论】:

      猜你喜欢
      • 2019-02-23
      • 1970-01-01
      • 2021-02-12
      • 2022-01-03
      • 1970-01-01
      • 1970-01-01
      • 2018-09-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多