Trash and untrash variations handling

Fixes #12774
This commit is contained in:
Mike Jolley 2017-01-11 15:41:19 +00:00
parent de11be2b6c
commit 80f79ae98d
3 changed files with 105 additions and 83 deletions

View File

@ -256,7 +256,6 @@ class WC_Post_Data {
break; break;
} }
} }
return $data; return $data;
} }
@ -266,56 +265,35 @@ class WC_Post_Data {
* @param mixed $id ID of post being deleted * @param mixed $id ID of post being deleted
*/ */
public static function delete_post( $id ) { public static function delete_post( $id ) {
global $woocommerce, $wpdb; if ( ! current_user_can( 'delete_posts' ) || ! $id ) {
if ( ! current_user_can( 'delete_posts' ) ) {
return; return;
} }
if ( $id > 0 ) { $post_type = get_post_type( $id );
$post_type = get_post_type( $id ); switch ( $post_type ) {
case 'product' :
switch ( $post_type ) { $data_store = WC_Data_Store::load( 'product-variable' );
case 'product' : $data_store->delete_variations( $id, true );
$child_product_variations = get_children( 'post_parent=' . $id . '&post_type=product_variation' );
if ( ! empty( $child_product_variations ) ) {
foreach ( $child_product_variations as $child ) {
wp_delete_post( $child->ID, true );
}
}
$child_products = get_children( 'post_parent=' . $id . '&post_type=product' );
if ( ! empty( $child_products ) ) {
foreach ( $child_products as $child ) {
$child_post = array();
$child_post['ID'] = $child->ID;
$child_post['post_parent'] = 0;
wp_update_post( $child_post );
}
}
if ( $parent_id = wp_get_post_parent_id( $id ) ) {
wc_delete_product_transients( $parent_id );
}
if ( $parent_id = wp_get_post_parent_id( $id ) ) {
wc_delete_product_transients( $parent_id );
}
break; break;
case 'product_variation' : case 'product_variation' :
wc_delete_product_transients( wp_get_post_parent_id( $id ) ); wc_delete_product_transients( wp_get_post_parent_id( $id ) );
break; break;
case 'shop_order' : case 'shop_order' :
$refunds = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = 'shop_order_refund' AND post_parent = %d", $id ) ); global $wpdb;
if ( ! is_null( $refunds ) ) { $refunds = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = 'shop_order_refund' AND post_parent = %d", $id ) );
foreach ( $refunds as $refund ) {
wp_delete_post( $refund->ID, true ); if ( ! is_null( $refunds ) ) {
} foreach ( $refunds as $refund ) {
wp_delete_post( $refund->ID, true );
} }
}
break; break;
}
} }
} }
@ -325,22 +303,28 @@ class WC_Post_Data {
* @param mixed $id * @param mixed $id
*/ */
public static function trash_post( $id ) { public static function trash_post( $id ) {
global $wpdb; if ( ! $id ) {
return;
}
if ( $id > 0 ) { $post_type = get_post_type( $id );
$post_type = get_post_type( $id ); // If this is an order, trash any refunds too.
if ( in_array( $post_type, wc_get_order_types( 'order-count' ) ) ) {
global $wpdb;
if ( in_array( $post_type, wc_get_order_types( 'order-count' ) ) ) { $refunds = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = 'shop_order_refund' AND post_parent = %d", $id ) );
$refunds = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = 'shop_order_refund' AND post_parent = %d", $id ) );
foreach ( $refunds as $refund ) { foreach ( $refunds as $refund ) {
$wpdb->update( $wpdb->posts, array( 'post_status' => 'trash' ), array( 'ID' => $refund->ID ) ); $wpdb->update( $wpdb->posts, array( 'post_status' => 'trash' ), array( 'ID' => $refund->ID ) );
}
delete_transient( 'woocommerce_processing_order_count' );
wc_delete_shop_order_transients( $id );
} }
wc_delete_shop_order_transients( $id );
// If this is a product, trash children variations.
} elseif ( 'product' === $post_type ) {
$data_store = WC_Data_Store::load( 'product-variable' );
$data_store->delete_variations( $id, false );
} }
} }
@ -350,32 +334,28 @@ class WC_Post_Data {
* @param mixed $id * @param mixed $id
*/ */
public static function untrash_post( $id ) { public static function untrash_post( $id ) {
global $wpdb; if ( ! $id ) {
return;
}
if ( $id > 0 ) { $post_type = get_post_type( $id );
$post_type = get_post_type( $id ); if ( in_array( $post_type, wc_get_order_types( 'order-count' ) ) ) {
global $wpdb;
if ( in_array( $post_type, wc_get_order_types( 'order-count' ) ) ) { $refunds = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = 'shop_order_refund' AND post_parent = %d", $id ) );
$refunds = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = 'shop_order_refund' AND post_parent = %d", $id ) ); foreach ( $refunds as $refund ) {
$wpdb->update( $wpdb->posts, array( 'post_status' => 'wc-completed' ), array( 'ID' => $refund->ID ) );
foreach ( $refunds as $refund ) {
$wpdb->update( $wpdb->posts, array( 'post_status' => 'wc-completed' ), array( 'ID' => $refund->ID ) );
}
delete_transient( 'woocommerce_processing_order_count' );
wc_delete_shop_order_transients( $id );
} elseif ( 'product' === $post_type ) {
// Check if SKU is valid before untrash the product.
$sku = get_post_meta( $id, '_sku', true );
if ( ! empty( $sku ) ) {
if ( ! wc_product_has_unique_sku( $id, $sku ) ) {
update_post_meta( $id, '_sku', '' );
}
}
} }
wc_delete_shop_order_transients( $id );
} elseif ( 'product' === $post_type ) {
$data_store = WC_Data_Store::load( 'product-variable' );
$data_store->untrash_variations( $id );
wc_product_force_unique_sku( $id );
} }
} }

View File

@ -283,7 +283,7 @@ class WC_Product_Variable_Data_Store_CPT extends WC_Product_Data_Store_CPT imple
public function child_is_in_stock( $product ) { public function child_is_in_stock( $product ) {
global $wpdb; global $wpdb;
$children = $product->get_visible_children( 'edit' ); $children = $product->get_visible_children( 'edit' );
$oufofstock_children = $wpdb->get_var( "SELECT COUNT( post_id ) FROM $wpdb->postmeta WHERE meta_key = '_stock_status' AND meta_value = 'instock' AND post_id IN ( " . implode( ',', array_map( 'absint', $children ) ) . " )" ); $oufofstock_children = $children ? $wpdb->get_var( "SELECT COUNT( post_id ) FROM $wpdb->postmeta WHERE meta_key = '_stock_status' AND meta_value = 'instock' AND post_id IN ( " . implode( ',', array_map( 'absint', $children ) ) . " )" ) : 0;
return $children > $oufofstock_children; return $children > $oufofstock_children;
} }
@ -378,18 +378,52 @@ class WC_Product_Variable_Data_Store_CPT extends WC_Product_Data_Store_CPT imple
/** /**
* Delete variations of a product. * Delete variations of a product.
* *
* @param WC_Product
* @since 2.7.0 * @since 2.7.0
* @param int $product_id
* @param $force_delete False to trash.
*/ */
public function delete_variations( &$product ) { public function delete_variations( $product_id, $force_delete = false ) {
$variations = $this->read_children( $product ); $variation_ids = wp_parse_id_list( get_posts( array(
'post_parent' => $product_id,
'post_type' => 'product_variation',
'fields' => 'ids',
'post_status' => 'any',
'numberposts' => -1,
) ) );
if ( ! empty( $variations['all'] ) ) { if ( ! empty( $variation_ids ) ) {
foreach ( $variations['all'] as $variation_id ) { foreach ( $variation_ids as $variation_id ) {
wp_delete_post( $variation_id, true ); if ( $force_delete ) {
wp_delete_post( $variation_id, true );
} else {
wp_trash_post( $variation_id );
}
} }
} }
delete_transient( 'wc_product_children_' . $product->get_id() ); delete_transient( 'wc_product_children_' . $product_id );
}
/**
* Untrash variations.
*
* @param int $product_id
*/
public function untrash_variations( $product_id ) {
$variation_ids = wp_parse_id_list( get_posts( array(
'post_parent' => $product_id,
'post_type' => 'product_variation',
'fields' => 'ids',
'post_status' => 'trash',
'numberposts' => -1,
) ) );
if ( ! empty( $variation_ids ) ) {
foreach ( $variation_ids as $variation_id ) {
wp_untrash_post( $variation_id );
}
}
delete_transient( 'wc_product_children_' . $product_id );
} }
} }

View File

@ -65,7 +65,15 @@ interface WC_Product_Variable_Data_Store_Interface {
/** /**
* Delete variations of a product. * Delete variations of a product.
* *
* @param WC_Product * @param int $product_id
* @param $force_delete False to trash.
*/ */
public function delete_variations( &$product ); public function delete_variations( $product_id, $force_delete = false );
/**
* Untrash variations.
*
* @param int $product_id
*/
public function untrash_variations( $product_id );
} }