Skip to content

Instantly share code, notes, and snippets.

@FrancoStino
Last active December 19, 2022 15:37
Show Gist options
  • Save FrancoStino/55ab6285cb24d390a1f49294fcdd35b5 to your computer and use it in GitHub Desktop.
Save FrancoStino/55ab6285cb24d390a1f49294fcdd35b5 to your computer and use it in GitHub Desktop.
Custom order status - Woocommerce
<?php
/**
* Rename "completed" Order Status to "shipped"
*/
add_filter( 'wc_order_statuses', 'bbloomer_rename_completed_order_status' );
function bbloomer_rename_completed_order_status( $statuses ) {
$statuses['wc-completed'] = 'Spedito';
return $statuses;
}
add_filter( 'woocommerce_register_shop_order_post_statuses', 'bbloomer_rename_completed_order_status_counter' );
function bbloomer_rename_completed_order_status_counter( $statuses ) {
$statuses['wc-completed']['label_count'] = _n_noop( 'Spedito <span class="count">(%s)</span>', 'Spediti <span class="count">(%s)</span>', 'woocommerce' );
return $statuses;
}
add_filter('bulk_actions-edit-shop_order', 'ts_bulk_actions_order_status', 20, 1);
function ts_bulk_actions_order_status($actions) {
$actions['mark_completed'] = __('Modifica lo stato in "Spedito"', 'woocommerce');
return $actions;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
* Auto-process WooCommerce orders except for BACS payments
*/
add_action( 'woocommerce_payment_complete', 'wpdesk_set_completed_for_paid_orders' );
function wpdesk_set_completed_for_paid_orders( $order_id ) {
$order = wc_get_order( $order_id );
$payment_method = $order->get_payment_method();
if( $payment_method == 'stripe' || $payment_method == 'ppcp-gateway' ){
$order->update_status( 'wc-order-accettato' );
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// register a custom post status 'awaiting-delivery' for Orders
add_action( 'init', 'register_custom_post_status', 20 );
function register_custom_post_status() {
register_post_status( 'wc-order-accettato', array(
'label' => _x( 'Accettato', 'Stato ordine', 'woocommerce' ),
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Accettato <span class="count">(%s)</span>', 'Accettati <span class="count">(%s)</span>', 'woocommerce' )
) );
}
// Adding custom status 'awaiting-delivery' to order edit pages dropdown
add_filter( 'wc_order_statuses', 'custom_wc_order_statuses' );
function custom_wc_order_statuses( $order_statuses ) {
$order_statuses['wc-order-accettato'] = _x( 'Accettato', 'Order status', 'woocommerce' );
return $order_statuses;
}
// Adding custom status 'awaiting-delivery' to admin order list bulk dropdown
add_filter( 'bulk_actions-edit-shop_order', 'custom_dropdown_bulk_actions_shop_order', 20, 1 );
function custom_dropdown_bulk_actions_shop_order( $actions ) {
$actions['mark_wc-order-accettato'] = __( 'Modifica lo stato in "Accettato"', 'woocommerce' );
return $actions;
}
// Adding action for 'awaiting-delivery'
add_filter( 'woocommerce_email_actions', 'custom_email_actions', 20, 1 );
function custom_email_actions( $actions ) {
$actions[] = 'woocommerce_order_status_wc-order-accettato';
return $actions;
}
add_action( 'woocommerce_order_status_wc-awaiting-delivery', array( WC(), 'send_transactional_email' ), 10, 1 );
// Sending an email notification when order get 'awaiting-delivery' status
add_action('woocommerce_order_status_awaiting-delivery', 'awaiting_delivery_order_status_email_notification', 20, 2);
function awaiting_delivery_order_status_email_notification( $order_id, $order ) {
// HERE below your settings
$heading = __('Accettato','woocommerce');
$subject = '[{site_title}] Il tuo ordine stato accettato: ({order_number}) - {order_date}';
// The email notification type
$email_key = 'WC_Email_Customer_Processing_Order';
// Get specific WC_emails object
$email_obj = WC()->mailer()->get_emails()[$email_key];
// Sending the customized email
$email_obj->trigger( $order_id );
}
// Customize email heading for this custom status email notification
add_filter( 'woocommerce_email_heading_customer_processing_order', 'email_heading_customer_wc_order_accettato', 10, 2 );
function email_heading_customer_wc_order_accettato( $heading, $order ){
if( $order->has_status( 'wc-order-accettato' ) ) {
$email_key = 'WC_Email_Customer_Processing_Order'; // The email notification type
$email_obj = WC()->mailer()->get_emails()[$email_key]; // Get specific WC_emails object
$heading_txt = __('Accettato','woocommerce'); // New heading text
return $email_obj->format_string( $heading_txt );
}
return $heading;
}
// Customize email subject for this custom status email notification
add_filter( 'woocommerce_email_subject_customer_processing_order', 'email_subject_customer_wc_order_accettato', 10, 2 );
function email_subject_customer_wc_order_accettato( $subject, $order ){
if( $order->has_status( 'wc-order-accettato' ) ) {
$email_key = 'WC_Email_Customer_Processing_Order'; // The email notification type
$email_obj = WC()->mailer()->get_emails()[$email_key]; // Get specific WC_emails object
$subject_txt = sprintf( __('[%s] Ordine accettato (%s) - %s', 'woocommerce'), '{site_title}', '{order_number}', '{order_date}' ); // New subject text
return $email_obj->format_string( $subject_txt );
}
return $subject;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment