1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
<?php
class DFW_Scheduled_Plan_Orders {
public static function create_orders_for_plans( $plan, $original_order_id, $item ) {
global $wpdb;
$plan_schedule = $plan->get_plan_schedule();
$total_percent = $plan->get_total_percent();
$price = ( $item['price'] * 100 ) / $total_percent;
$product_id = $item['product']->get_id();
$payment_no = 2;
$current_timestamp = current_time('timestamp');
$booking_query = '';
$booking_date = '';
$booking_time = '';
array_shift( $plan_schedule );
$booking_settings = get_post_meta( $product_id, 'woocommerce_booking_settings', true );
$is_booking_enabled = false;
$is_woo_booking_enabled = false;
if ( ( isset( $booking_settings['booking_enable_date'] ) && $booking_settings['booking_enable_date'] == "on" ) ) {
$is_booking_enabled = true;
}
if( class_exists( 'WC_Bookings' ) ) {
if( is_wc_booking_product( $item['product'] ) ) {
$is_woo_booking_enabled = true;
}
}
if( $is_booking_enabled === true ) {
$booking_query = "SELECT a1.start_date FROM `" . $wpdb->prefix . "booking_history` AS a1,`" . $wpdb->prefix . "booking_order_history` AS a2 WHERE a1.id = a2.booking_id AND a2.order_id=".$original_order_id;
$booking_date = $wpdb->get_results( $booking_query );
$booking_time = strtotime( $booking_date[0]->start_date, current_time('timestamp'));
} else if( $is_woo_booking_enabled ) {
$booking_query = "SELECT a1.meta_value FROM `" . $wpdb->prefix . "postmeta` as a1,`" . $wpdb->prefix . "posts` as a2 WHERE a1.post_id = a2.ID AND a2.post_parent = ". $original_order_id ." AND a1.meta_key = '_booking_start'";
$booking_date = $wpdb->get_results( $booking_query );
$booking_time = strtotime( $booking_date[0]->meta_value, current_time('timestamp'));
}
foreach ( $plan_schedule as $row ) {
if( "before" == $row->plan_due ) {
$current_timestamp = strtotime( "-{$row->plan_interval_amount} {$row->plan_interval_time}", $booking_time );
}else {
$current_timestamp = strtotime( "+{$row->plan_interval_amount} {$row->plan_interval_time}", current_time('timestamp') );
}
$item['amount'] = ( $price / 100 ) * $row->plan_amount;
DFW_Manage_Orders::dfw_create_order( $current_timestamp, $original_order_id, $payment_no, $item, 'scheduled-payment' );
$payment_no++;
}
}
}
$dfw_scheduled_plan_orders = new DFW_Scheduled_Plan_Orders();