WooCommerce Code Reference

ShippingMethodRestrictionsTrait

Lets a payment gateway restrict its availability to selected shipping methods.

Adds the "Enable for shipping methods" and "Accept for virtual orders" settings and enforces them in is_available(). The gateway must call init_shipping_method_restrictions() after init_settings() and merge get_shipping_method_restrictions_form_fields() into its form fields.

Tags
since
11.3.0

Table of Contents

$enable_for_methods  : array<string|int, mixed>
Enable for shipping methods.
$enable_for_virtual  : bool
Enable for virtual products.
get_shipping_method_options()  : array<string|int, mixed>
Get all of the shipping method options for the enable_for_methods field, grouped by shipping method.
is_available()  : bool
Check If The Gateway Is Available For Use.
get_matching_rates()  : array<string|int, mixed>
Indicates whether a rate exists in an array of canonically-formatted rate IDs that activates this gateway.
get_shipping_method_restrictions_form_fields()  : array<string|int, mixed>
Get the form fields for the shipping method restriction settings.
init_shipping_method_restrictions()  : void
Load the shipping method restriction settings into the gateway properties.
is_accessing_settings()  : bool
Checks to see whether or not the admin settings are being accessed by the current request.
load_shipping_method_options()  : array<string|int, mixed>
Loads all of the shipping method options for the enable_for_methods field.

Properties

Methods

get_shipping_method_options()

Get all of the shipping method options for the enable_for_methods field, grouped by shipping method.

public get_shipping_method_options() : array<string|int, mixed>

Unlike load_shipping_method_options(), this always returns the options, so only call it when they are actually needed. The zone lookup is expensive, so the result is cached for the rest of the request and shared by every gateway using this trait; saving a shipping zone or method invalidates it.

Tags
since
11.3.0
Return values
array<string|int, mixed> — Options keyed by shipping method title, each an array of rate id => option label.

is_available()

Check If The Gateway Is Available For Use.

public is_available() : bool
Tags
since
10.7.0

Added early return when gateway is disabled.

since
11.3.0

Moved here from WC_Gateway_COD. Only uses the order-pay context on pay-for-order page loads for an existing order.

Return values
bool —

get_matching_rates()

Indicates whether a rate exists in an array of canonically-formatted rate IDs that activates this gateway.

protected get_matching_rates(array<string|int, mixed> $rate_ids) : array<string|int, mixed>
Parameters
$rate_ids : array<string|int, mixed>

Rate ids to check.

Tags
since
3.4.0
since
11.3.0

Moved here from WC_Gateway_COD.

Return values
array<string|int, mixed> —

get_shipping_method_restrictions_form_fields()

Get the form fields for the shipping method restriction settings.

protected get_shipping_method_restrictions_form_fields() : array<string|int, mixed>
Tags
since
11.3.0
Return values
array<string|int, mixed> — Form fields keyed by setting id.

init_shipping_method_restrictions()

Load the shipping method restriction settings into the gateway properties.

protected init_shipping_method_restrictions() : void

Gateways that have never saved these settings default to no restriction.

Tags
since
11.3.0
Return values
void —

is_accessing_settings()

Checks to see whether or not the admin settings are being accessed by the current request.

protected is_accessing_settings() : bool
Tags
since
11.3.0

Moved here from WC_Gateway_COD.

Return values
bool —

load_shipping_method_options()

Loads all of the shipping method options for the enable_for_methods field.

protected load_shipping_method_options() : array<string|int, mixed>
Tags
since
11.3.0

Moved here from WC_Gateway_COD.

Return values
array<string|int, mixed> —