_x( 'Awaiting WhatsApp verification', 'Order status', 'orderproof' ),
'wc-codg-pending' => _x( 'Awaiting confirmation', 'Order status', 'orderproof' ),
'wc-codg-refused' => _x( 'Delivery refused', 'Order status', 'orderproof' ),
);
}
public static function register() {
$counts = array(
/* translators: %s: number of orders */
'wc-codg-otp' => _n_noop( 'Awaiting WhatsApp verification (%s)', 'Awaiting WhatsApp verification (%s)', 'orderproof' ),
/* translators: %s: number of orders */
'wc-codg-pending' => _n_noop( 'Awaiting confirmation (%s)', 'Awaiting confirmation (%s)', 'orderproof' ),
/* translators: %s: number of orders */
'wc-codg-refused' => _n_noop( 'Delivery refused (%s)', 'Delivery refused (%s)', 'orderproof' ),
);
foreach ( self::labels() as $status => $label ) {
register_post_status(
$status,
array(
'label' => $label,
'public' => false,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => $counts[ $status ],
)
);
}
}
public static function add_to_list( $statuses ) {
$new = array();
foreach ( $statuses as $key => $label ) {
$new[ $key ] = $label;
if ( 'wc-pending' === $key ) {
$new = array_merge( $new, array_slice( self::labels(), 0, 2 ) );
}
}
return array_merge( $new, self::labels() );
}
/**
* New COD orders go to "Awaiting confirmation" instead of "Processing".
* (WhatsApp OTP, when on, holds them in "Awaiting WhatsApp verification" first.)
*/
public static function cod_status( $status, $order ) {
return 'yes' === codg_opt( 'use_pending_status', 'yes' ) ? 'codg-pending' : $status;
}
/**
* The status an order should get once it is a real order (after OTP, or straight away without OTP).
*/
public static function confirmed_status() {
return 'yes' === codg_opt( 'use_pending_status', 'yes' ) ? 'codg-pending' : 'processing';
}
/**
* Transitions WooCommerce should treat as email triggers.
*/
private static function transitions() {
return array(
'new_order' => array( 'pending_to_codg-pending', 'codg-otp_to_codg-pending', 'codg-otp_to_processing' ),
'processing' => array( 'codg-otp_to_processing', 'codg-pending_to_processing' ),
);
}
public static function email_actions( $actions ) {
foreach ( self::transitions() as $list ) {
foreach ( $list as $t ) {
$actions[] = 'woocommerce_order_status_' . $t;
}
}
return array_unique( $actions );
}
/**
* Send the store's "New order" email when an order becomes real, and the customer's
* "Processing order" email when it is confirmed. WooCommerce only knows its own statuses.
*/
public static function hook_emails( $mailer ) {
$emails = array(
'new_order' => isset( $mailer->emails['WC_Email_New_Order'] ) ? $mailer->emails['WC_Email_New_Order'] : null,
'processing' => isset( $mailer->emails['WC_Email_Customer_Processing_Order'] ) ? $mailer->emails['WC_Email_Customer_Processing_Order'] : null,
);
foreach ( self::transitions() as $type => $list ) {
if ( ! $emails[ $type ] ) {
continue;
}
foreach ( $list as $t ) {
add_action( 'woocommerce_order_status_' . $t . '_notification', array( $emails[ $type ], 'trigger' ), 10, 2 );
}
}
}
public static function bulk_actions( $actions ) {
$actions['mark_codg-pending'] = __( 'Change status to awaiting confirmation', 'orderproof' );
$actions['mark_codg-refused'] = __( 'Change status to delivery refused', 'orderproof' );
return $actions;
}
/**
* Quick "Confirm" / "Refused" buttons in the orders list Actions column.
*/
public static function row_actions( $actions, $order ) {
if ( ! $order->has_status( 'codg-pending' ) ) {
return $actions;
}
$base = admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&order_id=' . $order->get_id() );
$actions['codg_confirm'] = array(
'url' => wp_nonce_url( $base . '&status=processing', 'woocommerce-mark-order-status' ),
'name' => __( 'Confirm', 'orderproof' ),
'action' => 'processing',
);
$actions['codg_refuse'] = array(
'url' => wp_nonce_url( $base . '&status=codg-refused', 'woocommerce-mark-order-status' ),
'name' => __( 'Refused', 'orderproof' ),
'action' => 'codg-refused',
);
return $actions;
}
}