Last active
March 21, 2017 08:55
-
-
Save niemenmaa/2504f6539b67efe417bd9b1382f72658 to your computer and use it in GitHub Desktop.
This snippet moves ordered items in draft state if they are in specific category when order is marked complete. Same without category filter: https://gist.github.com/Pikkulahti/545e5f738027c6e36637f49c6ccd2743
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 | |
/** | |
* Moves ordered items in draft state. | |
*/ | |
function custom_draft_complete_orders( $order_id ) { | |
// Fetch order infromation | |
$order = new WC_Order( $order_id ); | |
// Get products on order | |
$items = $order->get_items(); | |
foreach( $items as $product ) { | |
// Get product categories | |
$terms = get_the_terms( $product['product_id'], 'product_cat' ); | |
if( !empty($terms) ) { | |
// Array to store slugs into | |
$slugs = array(); | |
// Take slugs out of WP_Term objects | |
foreach ($terms as $category) { | |
$slugs[] = $category->slug; | |
} | |
// Category that product must belong to | |
$filter_category = 'shirts'; | |
// Loop through slugs | |
foreach ( $slugs as $slug ) { | |
// Check if product category matches the filter | |
if ( $filter_category === $slug ) { | |
// Change product status to draft | |
wp_update_post( | |
array( | |
'ID' => $product['product_id'], | |
'post_status' => 'draft', | |
) | |
); | |
break; | |
} | |
} | |
} | |
} | |
} | |
add_action( 'woocommerce_order_status_completed', 'custom_draft_complete_orders'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the
woocommerce_order_status_completed
hook only works for certain payment methods, so you need to figure out the right hooks accoring to your payment methods.