Gatemi Conditional Shipping Developer Reference

Gatemi Conditional Shipping applies no filters or actions of its own. It hooks three WooCommerce filters and stores its rules in a table shared with Gatemi Shipping for WooCommerce. Constants and classes use the GMCSP_ prefix.

WooCommerce hooks used

Hook Type Priority Does
woocommerce_package_rates filter 100 Removes shipping rates named by matching shipping rules
woocommerce_available_payment_gateways filter 100 Removes gateways named by matching payment rules; skipped inside wp-admin
woocommerce_no_available_payment_methods_message filter 10 Replaces the message with the matching payment rules' sentences

To run your own rate changes after the restrictions, hook woocommerce_package_rates at a priority above 100.

None of the three is added while WooCommerce is missing or while Gatemi Shipping (GMSHIP_VERSION) is active.

The table

{prefix}gmship_restrictions, created on activation with dbDelta() when the option gmship_schema_version is below 1. The statement is the same one Gatemi Shipping uses, so either plugin may create it.

Column Type Holds
restriction_id bigint unsigned, auto increment, primary key The rule's id
title varchar(200) The name
target_type varchar(20), default shipping shipping or payment
action varchar(20), default hide hide or show_only
targets longtext JSON list of method ids
conditions longtext JSON condition group
message varchar(255) The shopper sentence
sort_order smallint unsigned Reading order
enabled tinyint(1), default 1 1 active, 0 not

Index: KEY type_order (target_type, sort_order).

Rows are read once per request (ORDER BY sort_order, restriction_id) and kept in memory.

The condition group

{
  "match": "all",
  "rules": [
    { "type": "weight", "operator": "gte", "value": 20 },
    {
      "match": "any",
      "rules": [
        { "type": "country", "operator": "in", "value": ["GB", "IE"] },
        { "type": "shipping_class", "operator": "contains_any", "value": ["bulky"] }
      ]
    }
  ]
}
  • match is all (every entry must match) or any (one is enough).
  • An entry with rules or match is a nested group.
  • An empty rules list matches every checkout.
  • The screen writes one condition in an all group. Groups and several conditions come from Gatemi Shipping; this plugin judges them all the same.

Condition types this plugin judges

Type Value Operators
country List of ISO codes in, not_in
subtotal Number gte, lte, gt, lt, eq, neq, between ([low, high])
weight Number Same
item_count Number Same
shipping_class List of shipping-class term ids as strings; 0 means no class (a legacy slug is resolved to its id) contains_any, contains_all, contains_none, contains_only

The screen offers in, not_in, gte, lte, contains_any and contains_none; the others are read when Gatemi Shipping wrote them. An unknown numeric operator is read as gte.

Any other type is treated as matching (the strict reading), so a restriction written with a condition only Gatemi Shipping understands stays in force.

What is measured

Value Shipping rules Payment rules
Country $package['destination']['country'] WC()->customer->get_shipping_country()
Lines $package['contents'] WC()->cart->get_cart()
Subtotal Sum of line_subtotal Same
Weight get_weight() × quantity Same

Target matching

A method's id is WC_Shipping_Rate::get_id() for a rate and the gateway's id for a payment method. A target matches when it is:

  1. the exact id (flat_rate:3);
  2. a pattern with *, matched case-insensitively against the whole id (fedex*);
  3. the id's method part, ending at a colon (flat_rate matches flat_rate:3 but not flat_rate_plus:1).

Writing a rule from code

The plugin has no API for this; write a row the way the screen does.

global $wpdb;
$wpdb->insert(
	$wpdb->prefix . 'gmship_restrictions',
	array(
		'title'       => 'No COD abroad',
		'target_type' => 'payment',
		'action'      => 'hide',
		'targets'     => wp_json_encode( array( 'cod' ) ),
		'conditions'  => wp_json_encode( array(
			'match' => 'all',
			'rules' => array(
				array( 'type' => 'country', 'operator' => 'not_in', 'value' => array( 'GB' ) ),
			),
		) ),
		'message'     => 'Cash on delivery is only available in the UK.',
		'sort_order'  => 5,
		'enabled'     => 1,
	),
	array( '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%d' )
);

Options

Option Holds
gmship_schema_version The table's schema version, shared with Gatemi Shipping

Uninstall

uninstall.php deletes nothing — not the table, not the schema option — because the rows are the merchant's rules and the table is Gatemi Shipping's too.

CSS

The plugin loads no storefront CSS or JavaScript. The admin stylesheet assets/css/admin.css loads only on its own screen.

Shopping Cart
Scroll to Top