-
-
Save sc0ttkclark/d534dc980c49d92c2bf915e0cce9129b to your computer and use it in GitHub Desktop.
This snippet will add functionality that adds a new "Re-send Attendee Emails" action and bulk action for WooCommerce order management. Screencast: https://share.getcloudapp.com/jkuL6ZXr -- Also available: Regenerate Attendees https://gist.github.com/sc0ttkclark/35cdaaf59602d725625b0253349c66c3
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
<?php | |
// Register the action for the Edit order screen. | |
add_filter( 'woocommerce_order_actions', 'tec_event_tickets_plus_wc_register_resend_attendee_emails' ); | |
// Register the bulk action for the Orders screen. | |
add_filter( 'bulk_actions-edit-shop_order', 'tec_event_tickets_plus_wc_register_resend_attendee_emails' ); | |
/** | |
* Register the custom action to the list of order actions. | |
* | |
* @param array $actions List of order actions. | |
* | |
* @return array List of order actions with new action registered. | |
*/ | |
function tec_event_tickets_plus_wc_register_resend_attendee_emails( array $actions ) { | |
$actions['tec_event_tickets_plus_wc_resend_attendee_emails'] = __( 'Re-send Attendee Emails', 'event-tickets-plus' ); | |
return $actions; | |
} | |
/** | |
* Handle re-sending attendee emails for an order. | |
* | |
* @param Tribe__Tickets_Plus__Commerce__WooCommerce__Main $commerce_woo The Event Tickets Plus commerce provider for | |
* WooCommerce. | |
* @param WC_Order $order The WooCommerce order object. | |
*/ | |
function tec_event_tickets_plus_wc_resend_attendee_emails_for_order( Tribe__Tickets_Plus__Commerce__WooCommerce__Main $commerce_woo, WC_Order $order ) { | |
$order_id = $order->get_id(); | |
$commerce_woo->send_tickets_email( $order_id ); | |
$order->add_order_note( 'Re-sent attendee emails' ); | |
} | |
/** | |
* Add action for re-sending attendee emails for an order. | |
* | |
* @param $order WC_Order The WooCommerce order object. | |
*/ | |
add_action( 'woocommerce_order_action_tec_event_tickets_plus_wc_resend_attendee_emails', static function ( WC_Order $order ) { | |
/** @var Tribe__Tickets_Plus__Commerce__WooCommerce__Main $commerce_woo */ | |
$commerce_woo = tribe( 'tickets-plus.commerce.woo' ); | |
tec_event_tickets_plus_wc_resend_attendee_emails_for_order( $commerce_woo, $order ); | |
} ); | |
/** | |
* Handle bulk re-sending attendee emails for orders. | |
* | |
* @param string $redirect_to The URL to redirect to. | |
* @param string $action The bulk action name that is running. | |
* @param array $ids The list of Order ids. | |
* | |
* @return string The URL to redirect to. | |
*/ | |
add_filter( 'handle_bulk_actions-edit-shop_order', static function ( $redirect_to, $action, $ids ) { | |
if ( 'tec_event_tickets_plus_wc_resend_attendee_emails' !== $action ) { | |
return $redirect_to; | |
} | |
$ids = apply_filters( 'woocommerce_bulk_action_ids', array_reverse( array_map( 'absint', $ids ) ), $action, 'order' ); | |
if ( empty( $ids ) ) { | |
return $redirect_to; | |
} | |
/** @var Tribe__Tickets_Plus__Commerce__WooCommerce__Main $commerce_woo */ | |
$commerce_woo = tribe( 'tickets-plus.commerce.woo' ); | |
$changed = 0; | |
$report_action = 'tec_event_tickets_plus_wc_resent_attendee_emails'; | |
foreach ( $ids as $id ) { | |
$order = wc_get_order( $id ); | |
if ( ! $order ) { | |
continue; | |
} | |
tec_event_tickets_plus_wc_resend_attendee_emails_for_order( $commerce_woo, $order ); | |
$changed ++; | |
} | |
if ( $changed ) { | |
$args = [ | |
'post_type' => $commerce_woo->order_object, | |
'bulk_action' => $report_action, | |
'changed' => $changed, | |
'ids' => implode( ',', $ids ), | |
]; | |
$redirect_to = add_query_arg( $args, $redirect_to ); | |
} | |
return $redirect_to; | |
}, 9, 3 ); | |
/** | |
* Handle the admin notice success message. | |
*/ | |
add_action( 'admin_notices', static function () { | |
global $post_type, $pagenow; | |
// Bail out if not on shop order list page. | |
if ( 'edit.php' !== $pagenow || 'shop_order' !== $post_type || ! isset( $_REQUEST['bulk_action'] ) ) { // WPCS: input var ok, CSRF ok. | |
return; | |
} | |
$number = isset( $_REQUEST['changed'] ) ? absint( $_REQUEST['changed'] ) : 0; // WPCS: input var ok, CSRF ok. | |
$bulk_action = wc_clean( wp_unslash( $_REQUEST['bulk_action'] ) ); // WPCS: input var ok, CSRF ok. | |
if ( 'tec_event_tickets_plus_wc_resent_attendee_emails' !== $bulk_action ) { | |
return; | |
} | |
/* translators: %d: orders count */ | |
$message = sprintf( _n( '%d order has had attendee emails re-sent.', '%d orders have had their attendee emails re-sent.', $number, 'event-tickets-plus' ), number_format_i18n( $number ) ); | |
echo '<div class="updated"><p>' . esc_html( $message ) . '</p></div>'; | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment