WC_AJAX::add_variation()public staticWC 1.0

Add variation via ajax function.

Метод класса: WC_AJAX{}

Хуков нет.

Возвращает

null. Ничего (null).

Использование

$result = WC_AJAX::add_variation();

Код WC_AJAX::add_variation() WC 9.3.3

public static function add_variation() {
	check_ajax_referer( 'add-variation', 'security' );

	if ( ! current_user_can( 'edit_products' ) || ! isset( $_POST['post_id'], $_POST['loop'] ) ) {
		wp_die( -1 );
	}

	global $post; // Set $post global so its available, like within the admin screens.

	$product_id       = intval( $_POST['post_id'] );
	$post             = get_post( $product_id ); // phpcs:ignore
	$loop             = intval( $_POST['loop'] );
	$product_object   = wc_get_product_object( 'variable', $product_id ); // Forces type to variable in case product is unsaved.
	$variation_object = wc_get_product_object( 'variation' );
	$variation_object->set_parent_id( $product_id );
	$variation_object->set_attributes( array_fill_keys( array_map( 'sanitize_title', array_keys( $product_object->get_variation_attributes() ) ), '' ) );
	$variation_id   = $variation_object->save();
	$variation      = get_post( $variation_id );
	$variation_data = array_merge( get_post_custom( $variation_id ), wc_get_product_variation_attributes( $variation_id ) ); // kept for BW compatibility.
	include __DIR__ . '/admin/meta-boxes/views/html-variation-admin.php';
	wp_die();
}