Woocommerce - 以编程方式创建预订

时间:2018-03-13 13:43:17

标签: php wordpress woocommerce woocommerce-bookings

Woocommerce有这个代码,用于在同一天同一天的一周后自动创建另一个预订。

如何修改代码以使其执行4次相同的操作? ...即。从第一次预订开始,接下来的4周内每周同一次预订?

/** 
 * Code goes in theme functions.php
 *
 * In this example we're creating a booking 1 week after a booking is paid for.
 * This does not create another order or payment, just an additional booking.
 * $exact is false meaning if our slot is taken, the next available slot will be used.
 * @link https://docs.woocommerce.com/document/creating-bookings-programatically/
 */
function auto_create_followup_booking( $booking_id ) {

    // Get the previous booking from the ID
    $prev_booking = get_wc_booking( $booking_id );

    // Don't want follow ups for follow ups
    if ( $prev_booking->get_parent_id() <= 0 ) {
        // Set the follow up data
        $new_booking_data = array(
            'start_date'  => strtotime( '+1 week', $prev_booking->get_start() ), // same time, 1 week on
            'end_date'    => strtotime( '+1 week', $prev_booking->get_end() ),   // same time, 1 week on
            'resource_id' => $prev_booking->get_resource_id(),                   // same resource
            'parent_id'   => $booking_id,                                        // set the parent
        );
        // Did the previous booking have persons?
        $persons = $prev_booking->get_persons();
        if ( is_array( $persons ) && 0 < count( $persons ) ) {
            $new_booking_data['persons'] = $persons;
        }
        // Was the previous booking all day?
        if ( $prev_booking->is_all_day() ) {
            $new_booking_data['all_day'] = true;
        }
        create_wc_booking( 
            $prev_booking->get_product_id(), // Creating a booking for the previous bookings product
            $new_booking_data,               // Use the data pulled above
            $prev_booking->get_status(),     // Match previous bookings status
            false                            // Not exact, look for a slot
        );
    }
}

add_action( 'woocommerce_booking_in-cart_to_paid', 'auto_create_followup_booking' );
add_action( 'woocommerce_booking_unpaid_to_paid', 'auto_create_followup_booking' );
add_action( 'woocommerce_booking_confirmed_to_paid', 'auto_create_followup_booking' );

1 个答案:

答案 0 :(得分:1)

你可以这样做:

function auto_create_followup_booking( $booking_id ) {

    if($prev_booking->get_product_id() !== 1000){
         return;
    }

    // Get the previous booking from the ID
    $prev_booking = get_wc_booking( $booking_id );

    // Don't want follow ups for follow ups
    if ( $prev_booking->get_parent_id() <= 0 ) {
        // Set the follow up data
        for ($x = 1; $x <= 4; $x++) {
            $new_booking_data = array(
                'start_date'  => strtotime( '+'.$x.' week', $prev_booking->get_start() ), // same time, x week on
                'end_date'    => strtotime( '+'.$x.' week', $prev_booking->get_end() ),   // same time, x week on
                'resource_id' => $prev_booking->get_resource_id(),                   // same resource
                'parent_id'   => $booking_id,                                        // set the parent
            );
            // Did the previous booking have persons?
            $persons = $prev_booking->get_persons();
            if ( is_array( $persons ) && 0 < count( $persons ) ) {
                $new_booking_data['persons'] = $persons;
            }
            // Was the previous booking all day?
            if ( $prev_booking->is_all_day() ) {
                $new_booking_data['all_day'] = true;
            }
            create_wc_booking( 
                $prev_booking->get_product_id(), // Creating a booking for the previous bookings product
                $new_booking_data,               // Use the data pulled above
                $prev_booking->get_status(),     // Match previous bookings status
                false                            // Not exact, look for a slot
            );
        }

    }
}