WordPress at Your Fingertips
function is not described

WC_Product_Variable_Data_Store_CPT::delete_variations() public WC 3.0.0

Delete variations of a product.

{} It's a method of the class: WC_Product_Variable_Data_Store_CPT{}

Return

null. Nothing.

Usage

$WC_Product_Variable_Data_Store_CPT = new WC_Product_Variable_Data_Store_CPT();
$WC_Product_Variable_Data_Store_CPT->delete_variations( $product_id, $force_delete );
$product_id(int) (required)
Product ID.
$force_delete(true|false)
False to trash.
Default: false

Changelog

Since 3.0.0 Introduced.

Code of WC_Product_Variable_Data_Store_CPT::delete_variations() WC 5.6.0

public function delete_variations( $product_id, $force_delete = false ) {
	if ( ! is_numeric( $product_id ) || 0 >= $product_id ) {
		return;
	}

	$variation_ids = wp_parse_id_list(
		get_posts(
			array(
				'post_parent' => $product_id,
				'post_type'   => 'product_variation',
				'fields'      => 'ids',
				'post_status' => array( 'any', 'trash', 'auto-draft' ),
				'numberposts' => -1, // phpcs:ignore WordPress.VIP.PostsPerPage.posts_per_page_numberposts
			)
		)
	);

	if ( ! empty( $variation_ids ) ) {
		foreach ( $variation_ids as $variation_id ) {
			if ( $force_delete ) {
				do_action( 'woocommerce_before_delete_product_variation', $variation_id );
				wp_delete_post( $variation_id, true );
				do_action( 'woocommerce_delete_product_variation', $variation_id );
			} else {
				wp_trash_post( $variation_id );
				do_action( 'woocommerce_trash_product_variation', $variation_id );
			}
		}
	}

	delete_transient( 'wc_product_children_' . $product_id );
}