Last active
January 18, 2023 15:49
-
-
Save Drivingralle/959f71ed3ad5e12ee5047a0de31f1534 to your computer and use it in GitHub Desktop.
Add row/booking action button to decline a woocomerce booking
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Add decline button to list view of woocommerce bookings | |
*/ | |
function wocommerce_bookings_mods_add_custom_booking_status_button_to_bookings_list( $actions, $booking ) { | |
// Check if the booking requires a decision | |
if ( 'pending-confirmation' === $booking->get_status() ) { | |
// Add button to decline the booking | |
$actions['decline'] = array( | |
'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=wc-booking-decline&booking_id=' . $booking->get_id() ), 'wc-booking-decline' ), | |
'name' => __( 'Decline', 'wocommerce-bookings-mods' ), | |
'action' => 'decline', | |
); | |
} | |
// Return the array of actions | |
return $actions; | |
} | |
add_filter( 'woocommerce_admin_booking_actions', 'wocommerce_bookings_mods_add_custom_booking_status_button_to_bookings_list', 20, 2 ); | |
/* | |
* Add function to decline a woocomemrce booking via wp_ajax | |
*/ | |
function wocommerce_bookings_mods_wp_ajax_decline_booking() { | |
// Make sure the user is allowed to do this | |
if ( ! current_user_can( 'manage_bookings' ) ) { | |
wp_die( __( 'You do not have sufficient permissions to access this page.', 'woocommerce-bookings' ) ); | |
} | |
if ( ! check_admin_referer( 'wc-booking-decline' ) ) { | |
wp_die( __( 'You have taken too long. Please go back and retry.', 'woocommerce-bookings' ) ); | |
} | |
// Get the booking ID | |
$booking_id = isset( $_GET['booking_id'] ) && (int) $_GET['booking_id'] ? (int) $_GET['booking_id'] : ''; | |
if ( ! $booking_id ) { | |
die; | |
} | |
// Get the booking | |
$booking = get_wc_booking( $booking_id ); | |
// Check if the booking is not the target status | |
if ( 'declined' !== $booking->get_status() ) { | |
$booking->update_status( 'declined' ); | |
} | |
// Redirect to the bookings list | |
wp_safe_redirect( wp_get_referer() ); | |
} | |
add_action( 'wp_ajax_wc-booking-decline', 'wocommerce_bookings_mods_wp_ajax_decline_booking' ); | |
/* | |
* Add css to woocommerce bookings list view to make custom status visible | |
*/ | |
function wocommerce_bookings_mods_add_css_to_bookings_list() { | |
// Get the admin screen | |
$screen = get_current_screen(); | |
// Check if this is the bookings list | |
if ( 'edit' === $screen->base AND 'wc_booking' === $screen->post_type ) { | |
// Add custom css ?> | |
<style type="text/css"> | |
.type-wc_booking .column-booking_status span.status-declined:before { | |
color: #a00; | |
content: ""; | |
} | |
.column-booking_actions a.decline:after { | |
content: ""; | |
} | |
</style> | |
<?php } | |
} | |
add_action( 'admin_head', 'wocommerce_bookings_mods_add_css_to_bookings_list' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi i have been using this code and it has been working well, recently, i have been trying to use it however it takes me to the "You have taken too long. Please go back and retry."
im signed in as Admin. Tried activating and deactivating all plugins, no luck! any ideas?