【问题标题】:Get a checkout custom field value in a WooCommerce subscriptions order在 WooCommerce 订阅订单中获取结帐自定义字段值
【发布时间】:2018-02-06 02:29:02
【问题描述】:

为什么我不能从 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' 操作中无法从结帐字段中检索值吗?

【问题讨论】:

    标签: php wordpress woocommerce checkout woocommerce-subscriptions


    【解决方案1】:

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

    要从订阅对象获取父订单ID,您需要使用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 文件或任何插件文件中。

    我还没有真正测试过你的代码,但它不会抛出任何错误,这次应该可以为你工作。

    【讨论】:

    • 谢谢!!几个小时(几天?)我对此一无所获,你的建议解决了它。谢谢!!!
    • @aleks1217 这个很好……谢谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-17
    • 2021-07-01
    • 2019-04-14
    • 1970-01-01
    • 2020-09-14
    • 1970-01-01
    • 2023-03-22
    相关资源
    最近更新 更多