WC_AJAX::shipping_zone_add_method()public staticWC 1.0

Handle submissions from assets/js/wc-shipping-zone-methods.js Backbone model.

Method of the class: WC_AJAX{}

Return

null. Nothing (null).

Usage

$result = WC_AJAX::shipping_zone_add_method();

WC_AJAX::shipping_zone_add_method() code WC 8.9.0

public static function shipping_zone_add_method() {
	if ( ! isset( $_POST['wc_shipping_zones_nonce'], $_POST['zone_id'], $_POST['method_id'] ) ) {
		wp_send_json_error( 'missing_fields' );
		wp_die();
	}

	if ( ! wp_verify_nonce( wp_unslash( $_POST['wc_shipping_zones_nonce'] ), 'wc_shipping_zones_nonce' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
		wp_send_json_error( 'bad_nonce' );
		wp_die();
	}

	// Check User Caps.
	if ( ! current_user_can( 'manage_woocommerce' ) ) {
		wp_send_json_error( 'missing_capabilities' );
		wp_die();
	}

	$zone_id = wc_clean( wp_unslash( $_POST['zone_id'] ) );
	$zone    = new WC_Shipping_Zone( $zone_id );
	// A shipping zone can be created here if the user is adding a method without first saving the shipping zone.
	if ( '' === $zone_id ) {
		/**
		 * Notified that a non-option setting has been added.
		 *
		 * @since 7.8.0
		 */
		do_action(
			'woocommerce_update_non_option_setting',
			array(
				'id'     => 'shipping_zone',
				'action' => 'add',
			)
		);
	}
	/**
	 * Notify that a non-option setting has been added.
	 *
	 * @since 7.8.0
	 */
	do_action(
		'woocommerce_update_non_option_setting',
		array(
			'id'     => 'zone_method',
			'action' => 'add',
		)
	);
	$instance_id = $zone->add_shipping_method( wc_clean( wp_unslash( $_POST['method_id'] ) ) );

	global $current_tab;
	$current_tab = 'shipping';
	/**
	 * Completes the saving process for options.
	 *
	 * @since 7.8.0
	 */
	do_action( 'woocommerce_update_options' );

	wp_send_json_success(
		array(
			'instance_id' => $instance_id,
			'zone_id'     => $zone->get_id(),
			'zone_name'   => $zone->get_zone_name(),
			'methods'     => $zone->get_shipping_methods( false, 'json' ),
		)
	);
}