Created
October 24, 2016 06:55
-
-
Save bekarice/5233ed58c3a836064123b290463241c0 to your computer and use it in GitHub Desktop.
Add a WooCommerce custom order action
This file contains 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 // only copy if needed | |
/** | |
* Add a custom action to order actions select box on edit order page | |
* Only added for paid orders that haven't fired this action yet | |
* | |
* @param array $actions order actions array to display | |
* @return array - updated actions | |
*/ | |
function sv_wc_add_order_meta_box_action( $actions ) { | |
global $theorder; | |
// bail if the order has been paid for or this action has been run | |
if ( ! $theorder->is_paid() || get_post_meta( $theorder->id, '_wc_order_marked_printed_for_packaging', true ) ) { | |
return $actions; | |
} | |
// add "mark printed" custom action | |
$actions['wc_custom_order_action'] = __( 'Mark as printed for packaging', 'my-textdomain' ); | |
return $actions; | |
} | |
add_action( 'woocommerce_order_actions', 'sv_wc_add_order_meta_box_action' ); | |
/** | |
* Add an order note when custom action is clicked | |
* Add a flag on the order to show it's been run | |
* | |
* @param \WC_Order $order | |
*/ | |
function sv_wc_process_order_meta_box_action( $order ) { | |
// add the order note | |
$message = sprintf( __( 'Order information printed by %s for packaging.', 'my-textdomain' ), wp_get_current_user()->display_name ); | |
$order->add_order_note( $message ); | |
// add the flag so this action won't be shown again | |
update_post_meta( $order->id, '_wc_order_marked_printed_for_packaging', 'yes' ); | |
} | |
add_action( 'woocommerce_order_action_wc_custom_order_action', 'sv_wc_process_order_meta_box_action' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks!