Automattic\WooCommerce\Internal\RestApi\Routes\V4\ShippingZoneMethod

ShippingZoneMethodService::update_shipping_zone_methodpublicWC 9.4.0

Update a shipping method's properties.

Updates settings, enabled status, and/or sort order for a shipping method instance.

Method of the class: ShippingZoneMethodService{}

Returns

WC_Shipping_Method|\WP_Error. Updated method object on success, WP_Error on failure.

Usage

$ShippingZoneMethodService = new ShippingZoneMethodService();
$ShippingZoneMethodService->update_shipping_zone_method( $method, $instance_id, $data, $zone_id );
$method(WC_Shipping_Method) (required)
Shipping method instance to update.
$instance_id(int) (required)
Method instance ID from the database.
$data(array) (required)

Method properties to update. All parameters are optional.

  • settings(array)
    Settings to update (key-value pairs). See update_shipping_method_settings().

  • enabled(true|false)
    Whether the shipping method is enabled.

  • order(int)
    Sort order for displaying methods.
$zone_id(int|null)
Zone ID. Optional, but required for firing the status toggle hook.
Default: null

Changelog

Since 9.4.0 Introduced.

ShippingZoneMethodService::update_shipping_zone_method() code WC 10.4.3

public function update_shipping_zone_method( $method, $instance_id, $data, $zone_id = null ) {
	global $wpdb;

	$data = wp_parse_args(
		$data,
		array(
			'settings' => null,
			'enabled'  => null,
			'order'    => null,
		)
	);

	$updates         = array();
	$formats         = array();
	$enabled_changed = false;

	if ( ! is_null( $data['settings'] ) ) {
		$result = $this->update_shipping_method_settings( $method, $data['settings'] );
		if ( is_wp_error( $result ) ) {
			return $result;
		}
	}

	if ( ! is_null( $data['enabled'] ) ) {
		$updates['is_enabled'] = wc_string_to_bool( $data['enabled'] ) ? 1 : 0;
		$formats[]             = '%d';
		$method->enabled       = wc_string_to_bool( $data['enabled'] ) ? 'yes' : 'no';
		$enabled_changed       = true;
	}

	if ( ! is_null( $data['order'] ) ) {
		$updates['method_order'] = absint( $data['order'] );
		$formats[]               = '%d';
		$method->method_order    = absint( $data['order'] );
	}

	if ( empty( $updates ) ) {
		return $method;
	}

	$result = $wpdb->update(
		"{$wpdb->prefix}woocommerce_shipping_zone_methods",
		$updates,
		array( 'instance_id' => $instance_id ),
		$formats,
		array( '%d' )
	);

	if ( false === $result ) {
		return new WP_Error(
			'update_failed',
			__( 'Could not update shipping method.', 'woocommerce' )
		);
	}

	if ( $enabled_changed && null !== $zone_id ) {
		/**
		 * Fires when a shipping method's enabled status is toggled.
		 *
		 * @since 3.0.0
		 * @param int    $instance_id Instance ID of the shipping method.
		 * @param string $method_id   Shipping method ID (e.g., 'flat_rate').
		 * @param int    $zone_id     Zone ID.
		 * @param bool   $is_enabled  Whether the method is enabled.
		 */
		do_action(
			'woocommerce_shipping_zone_method_status_toggled',
			$instance_id,
			$method->id,
			$zone_id,
			(bool) $updates['is_enabled']
		);
	}

	WC_Cache_Helper::get_transient_version( 'shipping', true );
	return $method;
}