将支票付款方式插入 WooCommerce 订阅续订的正确方法

时间:2021-03-15 14:16:53

标签: wordpress woocommerce woocommerce-subscriptions

我正在尝试为支票订阅付款设置自动续订,而用户无需执行任何操作。到目前为止,一切都在我的代码段中工作,直到在没有用户交互的情况下为续订付款提供付款方式检查。知道我做错了什么吗?

add_action('woocommerce_subscription_status_updated','change_behaviour_for_cheque_payments_subscriptions');
add_action('woocommerce_scheduled_subscription_payment','change_behaviour_for_cheque_payments_subscriptions');
//add_action('init','change_behaviour_for_cheque_payments_subscriptions');
function change_behaviour_for_cheque_payments_subscriptions( $order_id,$payment_method = 'cheque') {
    $order_id = wc_get_order($order_id);  // Get an instance of the WC_Order object
    $subscription = new WC_Subscription($order_id);
    $related_orders_ids_array = $subscription->get_related_orders();
    $count_the_array = count($related_orders_ids_array);
    if($count_the_array === 0) {
        return;
    }
    if($count_the_array > 1 || $count_the_array == 1) {
        $index_position = $count_the_array -1;
    }
    $order_subs = wcs_get_subscriptions_for_order( $order_id);
    $values = array_values( $related_orders_ids_array);

    $get_core_parent_of_sub_renewal = $values[$index_position];
    if(empty($get_core_parent_of_sub_renewal)) {
        return;//exit
    }
    $get_latest_renewal_order_id = $values[0];
    if($count_the_array > 1) {
        if( wcs_order_contains_subscription( $get_core_parent_of_sub_renewal ) ){
            // Get an array of WC_Subscription objects
            $subscriptions = wcs_get_subscriptions_for_order( $get_core_parent_of_sub_renewal );
            foreach( $subscriptions as $subscription_id => $subscription ){
                // Change the status of the WC_Subscription object
                $subscription->update_status( 'active' );   
            }
            $get_latest_sub_renewal = $values[0];
            //$order = new WC_Order($get_latest_sub_renewal);
            //$order->update_status('completed');
            $order = wc_get_order( $get_latest_sub_renewal );
            $order->update_status( 'completed' );

             // Set payment gateway
             $payment_gateways = WC()->payment_gateways->payment_gateways();
             $order->set_payment_method( $payment_gateways['cheque'] );
    }
}
}

0 个答案:

没有答案