Forked from yratof/class-woocommerce-order-status.php
Created
August 13, 2020 18:15
-
-
Save eagleyed/816f74529c8083e63f56d71bff16a960 to your computer and use it in GitHub Desktop.
Custom order status for Woocommerce
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 | |
class order_status_customisation { | |
/** | |
* Setup actions | |
*/ | |
static function setup() { | |
add_action( 'init', __CLASS__ . '::adding_additional_order_statuses', 10 ); | |
add_filter( 'wc_order_statuses', __CLASS__ . '::add_awaiting_shipment_to_order_statuses', 10, 1 ); | |
add_action( 'admin_footer', __CLASS__ . '::status_styles', 10); | |
add_action( 'admin_footer', __CLASS__ . '::bulk_admin_footer', 10 ); | |
} | |
/** | |
* Status to add to woocommerce | |
* The KEY is the H in HSL. Thats a colour code | |
* it means HUE, SATURATIONS, LIGHTNESS. This will | |
* help differentiate between the order status'ssesss | |
* @return [type] [description] | |
*/ | |
static function order_statuses() { | |
$statuses = [ | |
96 => _x( 'Bytte', 'Order status' , 'craft-child-theme' ), | |
13 => _x( 'Exist', 'Order status' , 'craft-child-theme' ), | |
]; | |
return apply_filters( 'craft_order_status_array', $statuses ); | |
} | |
/** | |
* Style the status marks | |
*/ | |
static function status_styles() { | |
$statuses = self::order_statuses(); | |
if ( empty( $statuses ) ) { | |
return; | |
} | |
echo '<style>'; | |
foreach ( $statuses as $key => $status ) { | |
$name = self::stripper( $status ); | |
echo 'mark.' . $name . '::after { color: hsl( ' . $key . ', 70%, 50% ) }'; | |
} | |
foreach ( $statuses as $key => $status ) { | |
$name = self::stripper( $status ); | |
echo 'mark.' . $name . '::after,'; | |
} | |
?> | |
.marks-all-resolved::after { | |
content: "\e015"; | |
/* woo fonts */ | |
font-family: 'WooCommerce'; | |
speak: none; | |
font-weight: normal; | |
font-variant: normal; | |
text-transform: none; | |
line-height: 1; | |
-webkit-font-smoothing: antialiased; | |
margin: 0; | |
text-indent: 0; | |
position: absolute; | |
top: 0; | |
left: 0; | |
width: 100%; | |
height: 100%; | |
text-align: center; | |
} | |
<?php | |
echo '</style>'; | |
} | |
/** | |
* Clean up the names from the order statuses | |
* @param [type] $string [description] | |
* @return [type] [description] | |
*/ | |
static function stripper( $string ) { | |
//Lower case everything | |
$string = strtolower( $string ); | |
//Make alphanumeric ( removes all other character s) | |
$string = preg_replace( "/[^a-z0-9_\s-]/", "", $string ); | |
//Clean up multiple dashes or whitespaces | |
$string = preg_replace( "/[\s-]+/", " ", $string ); | |
//Convert whitespaces and underscore to dash | |
$string = preg_replace( "/[\s_]/", "-", $string ); | |
return $string; | |
} | |
/** | |
* Register a batch of new order status | |
**/ | |
static function adding_additional_order_statuses() { | |
$statuses = self::order_statuses(); | |
if ( empty( $statuses ) ) { | |
return; | |
} | |
foreach ( $statuses as $status ) { | |
$name = 'wc-' . self::stripper( $status ); | |
register_post_status( $name, [ | |
'label' => $status, | |
'public' => true, | |
'exclude_from_search' => false, | |
'show_in_admin_all_list' => true, | |
'show_in_admin_status_list' => true, | |
'label_count' => _n_noop( $status . '<span class="count">(%s)</span>', $status . '<span class="count">(%s)</span>' ) | |
] ); | |
} | |
} | |
/** | |
* Add to list of WC Order statuses | |
* @param [array] $order_statuses [return a list of the order statuses] | |
*/ | |
static function add_awaiting_shipment_to_order_statuses( $order_statuses ) { | |
$new_order_statuses = []; | |
// Add new order status after processing | |
foreach ( $order_statuses as $key => $status ) { | |
$new_order_statuses[ $key ] = $status; | |
if ( 'wc-processing' === $key ) { | |
$custom_statuses = self::order_statuses(); | |
foreach ( $custom_statuses as $custom_status ) { | |
$name = 'wc-' . self::stripper( $custom_status ); | |
$new_order_statuses[ $name ] = $custom_status; | |
} | |
} | |
} | |
return $new_order_statuses; | |
} | |
/** | |
* Add actions for the orders | |
* @return [type] [description] | |
*/ | |
static function bulk_admin_footer() { | |
global $post_type; | |
if ( 'shop_order' == $post_type ) { | |
?> | |
<script type="text/javascript"> | |
jQuery( function() { | |
<?php | |
$custom_statuses = self::order_statuses(); | |
foreach ( $custom_statuses as $custom_status ) { | |
$name = self::stripper( $custom_status ); | |
?> | |
jQuery( '<option>' ).val( 'mark_<?php echo $name; ?>' ).text( '<?php echo _x( 'Mark as', 'Mark as processing or completed', 'craft-child-theme' ) . ' ' . $custom_status ?>' ).appendTo( 'select[name="action"]' ); | |
jQuery( '<option>' ).val( 'mark_<?php echo $name; ?>' ).text( '<?php echo _x( 'Mark as', 'Mark as processing or completed', 'craft-child-theme' ) . ' ' . $custom_status ?>' ).appendTo( 'select[name="action2"]' ); | |
<?php } ?> | |
}); | |
</script> | |
<?php | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment