在 WooCommerce 订阅订单中获取结帐自定义字段值

Get a checkout custom field value in a WooCommerce subscriptions order

为什么我无法从 Woocommerce 结帐字段中检索数据以在订单处理过程中更新订阅数据?

我想根据客户结帐时的频率请求更新下一个续订日期。这是我的代码:

1) 设置结帐字段:

add_action( 'woocommerce_after_order_notes', 'hgf_custom_checkout_field' );

function hgf_custom_checkout_field( $checkout ) {
global $woocommerce;

    woocommerce_form_field( 'frequency', array(
        'type'          => 'select',
        'required'          => true,
        'class'         => array('form-row-wide'),
        'label'         => __('Change this any time, just email.'),
        'options'     => array(
            'blank'     => __( 'Select frequency preference', 'woocommerce' ),              
            1 => __( 'Every Week', 'woocommerce' ),
            2 => __( 'Every Other Week' , 'woocommerce' ),
            3 => __( 'Once Per Month' , 'woocommerce' )
                )
            ), $checkout->get_value( 'frequency' ));
            echo '</div>';
}

2) 更新订单元数据。这将在客户订单上创建两个自定义字段,并更新订阅的计费间隔:

add_action( 'woocommerce_checkout_update_order_meta', 'hgf_checkout_field_update_order_meta' );
function hgf_checkout_field_update_order_meta( $order_id ) {

    if ( ! empty( $_POST['frequency'] ) ) {

        update_post_meta( $order_id, 'frequency', $_POST['frequency'] );
        update_post_meta( $order_id, 'billing_interval', $_POST['frequency'] );
    }

}

3) 更新续订日期
对于最后一部分,我可以使用我选择的虚拟日期更新续订日期(例如:$renewal date = "2018-12-15",但是当我尝试让它读取 'billing_interval' 字段时,它会恢复为 else默认。

    add_action( 'woocommerce_checkout_create_subscription', 'hgf_next_renewal' );
    function hgf_next_renewal( $subscription, $timezone='site' ) {

        $billing_interval = get_post_meta( get_the_ID(), 'billing_interval', true); 

    if( $billing_interval == 2 ) {
        $renewal_date = date( 'Y-m-d H:i:s', strtotime( "2017-10-01" ) ) /* date( 'Y-m-d H:i:s', strtotime( "+2 weeks" ) ) */ ;
    } if( $billing_interval == 4 ) {
        $renewal_date = date( 'Y-m-d H:i:s', strtotime( "2017-12-01" ) ) /* date( 'Y-m-d H:i:s', strtotime( "+4 weeks" ) ) */ ;
    } else {
        $renewal_date = date( 'Y-m-d H:i:s' ) ) /* date( 'Y-m-d H:i:s', strtotime( $renewal_date) ) */ ;
    }

        $subscription->update_dates( array(
            'next_payment' => $renewal_date,
            ) );

        return $subscription;
    }

这是我尝试过的所有不同方式:

1) $billing_interval = $subscription->get_billing_interval();
2) $billing_interval = get_post_meta( $subscription->get_ID(), 'billing_interval', true);
3) $billing_interval = get_post_meta( $order_id, 'billing_interval', true);
4) $subscriptions = wcs_get_users_subscriptions( $user_id );
     foreach ( $subscriptions as $sub ) {
      $billing_interval = get_post_meta( $sub->get_order_number(), '_billing_interval', true);
      }
5) $billing_interval = $checkout->get_value( 'frequency' );

有谁知道为什么我在 'woocommerce_checkout_create_subscription' 操作中无法从我的结帐字段中检索值?

当您在订单元数据中保存您的 'billing_interval' 自定义字段时,您无法通过订阅对象或 ID 获取此自定义字段...您需要首先获取 父订单 ID

To get the parent Order ID from the subscription object you need to use the WC_Order method get_parent_id()

add_action( 'woocommerce_checkout_create_subscription', 'hgf_next_renewal' );
function hgf_next_renewal( $subscription, $timezone='site' ) {
    
    // MISSING!: The parent order ID to set below
    $order_parent_id = $subscription->get_parent_id();
    
    $billing_interval = get_post_meta( $order_parent_id, 'billing_interval', true);

    if( $billing_interval == 2 ) {
        $renewal_date = date( 'Y-m-d H:i:s', strtotime( "2017-10-01" ) ) /* date( 'Y-m-d H:i:s', strtotime( "+2 weeks" ) ) */ ;
    } elseif( $billing_interval == 4 ) {
        $renewal_date = date( 'Y-m-d H:i:s', strtotime( "2017-12-01" ) ) /* date( 'Y-m-d H:i:s', strtotime( "+4 weeks" ) ) */ ;
    } else {
        $renewal_date = date( 'Y-m-d H:i:s', strtotime( "2017-09-01" ) ) /* date( 'Y-m-d H:i:s' ) */ ;
    }

    $subscription->update_dates( array(
        'next_payment' => $renewal_date,
    ) );

    return $subscription;
}

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。

我还没有真正测试过你的代码,但它没有抛出任何错误,这次应该对你有用。