WC_AJAX::shipping_zones_save_changes()public staticWC 1.0

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

Method of the class: WC_AJAX{}

Return

null. Nothing (null).

Usage

$result = WC_AJAX::shipping_zones_save_changes();

WC_AJAX::shipping_zones_save_changes() code WC 8.7.0

public static function shipping_zones_save_changes() {
	if ( ! isset( $_POST['wc_shipping_zones_nonce'], $_POST['changes'] ) ) {
		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();
	}

	$changes = wp_unslash( $_POST['changes'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
	foreach ( $changes as $zone_id => $data ) {
		if ( isset( $data['deleted'] ) ) {
			if ( isset( $data['newRow'] ) ) {
				// So the user added and deleted a new row.
				// That's fine, it's not in the database anyways. NEXT!
				continue;
			}
			/**
			 * Notify that a non-option setting has been deleted.
			 *
			 * @since 7.8.0
			 */
			do_action(
				'woocommerce_update_non_option_setting',
				array(
					'id'     => 'shipping_zone',
					'action' => 'delete',
				)
			);
			WC_Shipping_Zones::delete_zone( $zone_id );
			continue;
		}

		$zone_data = array_intersect_key(
			$data,
			array(
				'zone_id'    => 1,
				'zone_order' => 1,
			)
		);

		if ( isset( $zone_data['zone_id'] ) ) {
			$zone = new WC_Shipping_Zone( $zone_data['zone_id'] );

			if ( isset( $zone_data['zone_order'] ) ) {
				/**
				 * Notify that a non-option setting has been updated.
				 *
				 * @since 7.8.0
				 */
				do_action(
					'woocommerce_update_non_option_setting',
					array(
						'id' => 'zone_order',
					)
				);
				$zone->set_zone_order( $zone_data['zone_order'] );
			}
			$zone->save();
		}
	}

	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(
			'zones' => WC_Shipping_Zones::get_zones( 'json' ),
		)
	);
}