Skip to content

Instantly share code, notes, and snippets.

@jessepearson
Last active August 7, 2023 14:47
Show Gist options
  • Select an option

  • Save jessepearson/006d4b7d480a322367ac21573c5d72ff to your computer and use it in GitHub Desktop.

Select an option

Save jessepearson/006d4b7d480a322367ac21573c5d72ff to your computer and use it in GitHub Desktop.
Place Bookings into a confirmed status if they have been paid for via COD - for WooCommerce Bookings
<?php // only copy this line if needed
// related blog post: https://jessepearson.net/2016/11/woocommerce-bookings-cod-payments/
/**
* Function that will put bookings into a Confirmed status if they were paid for via COD
*
* @param int/string $order_id The order id
* @return null
*/
function lets_confirm_some_cod_bookings( $order_id ) {
global $wpdb;
$order = wc_get_order( $order_id );
// ONLY do this for COD orders
if ( $order->has_status( 'processing' ) && 'cod' !== $order->payment_method ) {
return;
}
$bookings = array();
foreach ( $order->get_items() as $order_item_id => $item ) {
if ( 'line_item' == $item['type'] ) {
$bookings = array_merge( $bookings, $wpdb->get_col( $wpdb->prepare( "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_booking_order_item_id' AND meta_value = %d", $order_item_id ) ) );
}
}
// add filters so we don't change back from completed statuses
add_filter( 'woocommerce_bookings_for_user_statuses', 'lets_filter_off_complete_statuses', 20, 1 );
add_filter( 'woocommerce_valid_booking_statuses_for_cancel', 'lets_filter_off_complete_statuses', 20, 1 );
add_filter( 'woocommerce_bookings_fully_booked_statuses', 'lets_filter_off_complete_statuses', 20, 1 );
foreach ( $bookings as $booking_id ) {
$booking = get_wc_booking( $booking_id );
$booking->update_status( 'confirmed' );
}
// and then remove the filters
remove_filter( 'woocommerce_bookings_for_user_statuses', 'lets_filter_off_complete_statuses', 20, 1 );
remove_filter( 'woocommerce_valid_booking_statuses_for_cancel', 'lets_filter_off_complete_statuses', 20, 1 );
remove_filter( 'woocommerce_bookings_fully_booked_statuses', 'lets_filter_off_complete_statuses', 20, 1 );
}
add_action( 'woocommerce_order_status_processing', 'lets_confirm_some_cod_bookings', 20, 1 );
add_action( 'woocommerce_order_status_completed', 'lets_confirm_some_cod_bookings', 20, 1 );
/**
* Create a filter to remove certain statuses from the array
*
* @param array $statuses The array of valid statuses
* @return array The modified status array
*/
function lets_filter_off_complete_statuses( $statuses ) {
if ( ! empty( $statuses['paid'] ) ) {
unset( $statuses['paid'] );
}
if ( ! empty( $statuses['complete'] ) ) {
unset( $statuses['complete'] );
}
return $statuses;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment