Skip to content

Instantly share code, notes, and snippets.

@scotcrop
Last active May 8, 2019 05:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scotcrop/a7c26aba05973e12a6b5 to your computer and use it in GitHub Desktop.
Save scotcrop/a7c26aba05973e12a6b5 to your computer and use it in GitHub Desktop.
Adding and updating a custom shop order post status when upgrading WooCommerce 2.1 and below to 2.2 and up
<?php
/*
Plugin Name: Custom Order Status for WooCommerce Shop Orders
Plugin URI: http://scotcrop.com/2014/10/11/adding-and-updating-a-custom-shop-order-post-status-when-upgrading-woocommerce-2-1-and-below-to-2-2-and-up/
Description: Creates a new WooCommerce Order Status called In-Production and updates shop orders that were converted to a post status of 'publish' after upgrading from WooCommerce 2.1 and earlier to 2.2 and later.
Version: 1.0
Author: Scott Cropper
Author URI: http://scotcrop.com
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
// Register a new post status to be used for WooCommmerce Orders
add_action( 'init', 'register_in_production_order_status' );
// Add a new order status to list of WC_Order statuses
add_filter( 'wc_order_statuses', 'add_in_production_order_status' );
// Run the update function when activated
register_activation_hook( __FILE__, 'update_published_orders' );
function register_in_production_order_status() {
$args = array(
'label' => 'In-Production',
'public' => true,
'label_count' => _n_noop( 'In-Production <span class="count">(%s)</span>', 'In-Production <span class="count">(%s)</span>' ),
);
register_post_status( 'wc-in-production', $args );
}
function add_in_production_order_status( $order_statuses ) {
$updated_order_statuses = array();
foreach ( $order_statuses as $key => $status ) {
$updated_order_statuses[ $key ] = $status;
// Check for and insert after 'Processing' order status
if ( 'wc-processing' === $key ) {
$updated_order_statuses['wc-in-production'] = 'In-Production';
}
}
return $updated_order_statuses;
}
function update_published_orders() {
// Adding post_status => 'publish' to these args would narrow the results but
// you run into an issue with a WooCommerce filter trying to help and getting in the way
// See
// http://docs.woothemes.com/wc-apidocs/source-function-wc_shop_order_status_backwards_compatibility.html#669-715
$args = array(
'post_type' => 'shop_order',
'posts_per_page' => -1,
);
$shop_orders = new WP_Query( $args );
if ( $shop_orders->have_posts() ) {
while ( $shop_orders->have_posts() ) {
$shop_orders->the_post();
$post_id = get_the_id();
if ( 'publish' == get_post_status( $post_id ) ) {
$shop_order = array(
'ID' => $post_id,
'post_status' => 'wc-in-production',
);
wp_update_post( $shop_order );
}
}
}
wp_reset_postdata();
}
@scotcrop
Copy link
Author

For some additional background on why this code is useful check out - http://develop.woothemes.com/woocommerce/2014/08/wc-2-2-order-statuses-plugin-compatibility/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment