Rateline documentation1.x
Buy Pro· 5 sites · $159/year
Developers

Filters and hooks

Add conditions, rule fields, columns and rates to the Table rate method from code.

Rateline's hooks are WordPress filters and actions. Put the examples in a small custom plugin or your child theme's functions.php. Rateline Pro is built on these same hooks.

How a rate is worked out

  1. Method::lines() turns the package's items that need shipping into lines: qty, weight (each), total (the line as customers see it) and class (0 for none). rateline_package_line can add more.
  2. Rules::rates() takes the table's first rate and finds the first rule that matches the whole order: its shipping class fits, its condition's measure is in range (rateline_measure gives the measures), and rateline_rule_matches agrees.
  3. rateline_rates can replace or add rates.
  4. The method adds the handling fee, minimum and maximum, and applies free shipping coupons, to each rate.

A condition of your own

A condition needs a label for the When column and a measure of the same key. This one checks the tallest item, from each product's height:

// The height of each cart line's product.
add_filter( 'rateline_package_line', function ( $line, $item, $product ) {
	$line['height'] = (float) $product->get_height();
	return $line;
}, 10, 3 );

// The tallest item in the lines being priced.
add_filter( 'rateline_measure', function ( $measure, $lines ) {
	$measure['height'] = max( array_merge( array( 0 ), array_column( $lines, 'height' ) ) );
	return $measure;
}, 10, 2 );

// In the When column.
add_filter( 'rateline_conditions', function ( $conditions ) {
	$conditions['height'] = 'Tallest item (cm)';
	return $conditions;
} );

Rateline's own fields and measures (qty, weight, total, class; weight, items, subtotal) can't be replaced.

A rule field and a check of your own

Register the field, show it as a column, and check it:

add_filter( 'rateline_rule_fields', function ( $fields ) {
	return $fields + array( 'country' => '' );
} );

add_filter( 'rateline_rule_columns', function ( $columns ) {
	$columns['country'] = array(
		'title'  => 'Country',
		'class'  => 'my-col-country',
		'fields' => array( 'country' ),
		'cell'   => function ( $name, $rule ) {
			return '<td><input type="text" name="' . esc_attr( $name . '[country]' ) . '" value="' . esc_attr( $rule['country'] ) . '"></td>';
		},
	);
	return $columns;
} );

add_filter( 'rateline_rule_matches', function ( $matches, $rule, $package ) {
	return $matches && ( '' === $rule['country'] || $rule['country'] === ( $package['destination']['country'] ?? '' ) );
}, 10, 3 );

rateline_clean_rule gets each rule as it's saved, to tidy your field. Columns are drawn in array order, between the drag handle and the remove button; a field without a column is kept in a hidden input.

Warning

A rule with a value in a field no active plugin registers doesn't match. That keeps a table safe when the plugin that added the field is deactivated: its rules are skipped rather than applied to every order.

Rates

rateline_rates gets the rates Rateline worked out (name => array( 'cost' => float, 'label' => string ), where '' is the method's own) with the rules, lines, package and method. Return more rates, or other costs:

// A weekend surcharge on the method's own rate.
add_filter( 'rateline_rates', function ( $rates ) {
	if ( isset( $rates[''] ) && in_array( gmdate( 'N' ), array( '6', '7' ), true ) ) {
		$rates['']['cost'] += 5;
	}
	return $rates;
} );

A named rate gets the rate ID rateline:{instance}:{name}; its label is the rule's label, else the name.

  • rateline_method_fields gets the method's settings fields (WooCommerce settings API), in order, and the method. Read your own with $method->get_option( 'key' ).
  • rateline_rules_footer fires at the end of the rules table's footer, after + Add rule.

When Rateline is loaded

rateline_loaded fires once Rateline has loaded (on plugins_loaded, only when WooCommerce is active). Hook your code there rather than checking for Rateline's classes. RATELINE_VERSION holds its version.

Where the settings are

Each method's settings are in the option woocommerce_rateline_{instance}_settings, with the rules under rules as a list of arrays: rate, condition, min, max, class, cost, per_item, per_unit, percent, label, hide, and any fields add-ons register.