1

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

如何修改代码以使其执行 4 次相同的操作?...IE。在第一次预订后的 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' );
4

1 回答 1

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
            );
        }

    }
}
于 2018-03-13T14:27:42.040 回答