Woocommerce - Cannot delete a product variation

2019-08-05 00:45发布

问题:

Using WooCommerce 2.6.1

I cannot delete a product variation for a variable product: the record is still in the database after the ajax call. It seems the ajax call doesn't go through: putting error_log(print_r('remove_variation', true)); doesn't output anything (line 387 in class-wc-ajax.php). The action is added in the constructor of the class. The function public function remove_variation() is just not called.

Has anyone had the same issue, and found a way to make it work?

回答1:

/**
* Trash a variation, don't delete it permanently.
*
* This is hooked to
* Hijack WooCommerce's WC_AJAX::remove_variation() "Delete Variation" Trash a variation if it is a subscription variation via ajax function
*/
public static function remove_variations() {

if ( isset( $_POST['variation_id'] ) ) { // removing single variation
error_log("here3");

check_ajax_referer( 'delete-variation', 'security' );
$variation_ids = array( $_POST['variation_id'] );
error_log($_POST['variation_id']);

} else { // removing multiple variations
error_log("here4");

check_ajax_referer( 'delete-variations', 'security' );
$variation_ids = (array) $_POST['variation_ids'];

}

foreach ( $variation_ids as $variation_id ) {

$variation_post = get_post( $variation_id );
error_log(print_r($variation_post, ));

if ( $variation_post && $variation_post->post_type == 'product_variation' ) {

$variation_product = get_product( $variation_id );

if ( $variation_product && $variation_product->is_type( 'subscription_variation' ) ) {
wp_trash_post( $variation_id );
}
}
}
die();
}

remove && $variation_product->is_type( 'subscription_variation' ) to solve the problem of un-deletable variations. http://support.woothemes.com/requests/162693 should provide a patch, issue has been reported.