WC_AJAX::add_attributes_and_variations()public staticWC 1.0

Save attributes and variations via ajax.

Method of the class: WC_AJAX{}

No Hooks.

Return

null. Nothing (null).

Usage

$result = WC_AJAX::add_attributes_and_variations();

WC_AJAX::add_attributes_and_variations() code WC 9.0.1

public static function add_attributes_and_variations() {
	check_ajax_referer( 'add-attributes-and-variations', 'security' );

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

	try {
		parse_str( wp_unslash( $_POST['data'] ), $data ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized

		$product = self::create_product_with_attributes( $data );
		self::create_all_product_variations( $product );

		wp_send_json_success();
		wp_die();

	} catch ( Exception $e ) {
		wp_send_json_error( array( 'error' => $e->getMessage() ) );
	}
}