Shipping Calculator developer reference

Filters, actions, template functions and templates for placing and restyling the shipping calculator.

Beka Rice Avatar

Written by

Last updated


Shipping Calculator’s filters, actions, template functions and templates let developers place the calculator anywhere, change where it shows, and restyle or rewrite its output. Add the snippets below in a code snippets plugin or your child theme’s functions.php. For everyday setup, see Get started with Shipping Calculator.

When you’d use this

  • Put the calculator in a spot the Position setting doesn’t offer, such as above the price.
  • Hide the calculator on some products, such as gift cards or made-to-order items.
  • Let shoppers pick a shipping option in the calculator.
  • Change the calculator’s markup or messages.

Place the calculator in code

With the template function

Call wsc_display_shipping_calculator() where you want the calculator in a child theme template. It outputs nothing on pages where the calculator can’t show, such as a virtual product.

For example, to show it above the price in Storefront, copy woocommerce/templates/single-product/price.php to storefront-child/woocommerce/single-product/price.php and add the call before the price:

<?php
global $product;

wsc_display_shipping_calculator();
?>
<p class="<?php echo esc_attr( apply_filters( 'woocommerce_product_price_class', 'price' ) ); ?>"><?php echo $product->get_price_html(); ?></p>

Set Position to Don’t display the shipping calculator automatically so it doesn’t also appear in its usual place.

With a hook and priority

For the product summary positions, wsc_shipping_calculator_position_action and wsc_shipping_calculator_position_priority change the action and priority the calculator is added to. In WooCommerce’s standard layout, the price is added to woocommerce_single_product_summary at priority 10, so priority 9 puts the calculator just above it:

add_filter( 'wsc_shipping_calculator_position_action', function( $action ) {
	return 'woocommerce_single_product_summary';
} );

add_filter( 'wsc_shipping_calculator_position_priority', function( $priority ) {
	return 9;
} );

These only apply when Position is one of the product summary positions. Many themes fire their own actions around the price; Astra, for example, fires astra_woo_single_price_before and astra_woo_single_price_after.

As a tab next to any tab

Return a position of before_<tab>_tab or after_<tab>_tab from wsc_setting_position, where <tab> is the key of an existing product tab (description, additional_information, reviews, or a tab added by another plugin):

add_filter( 'wsc_setting_position', function( $value, $setting_id ) {
	return 'before_reviews_tab';
}, 10, 2 );

Filters

wsc_setting_{setting}

Filters a setting’s value: wsc_setting_label, wsc_setting_position or wsc_setting_expand. Arguments: the value, then the setting ID. This example shows the calculator above the add to cart button on products in one category, and leaves the setting alone elsewhere:

add_filter( 'wsc_setting_position', function( $value, $setting_id ) {
	if ( is_product() && has_term( 'furniture', 'product_cat' ) ) {
		return 'above_add_to_cart';
	}

	return $value;
}, 10, 2 );

Position values: none, above_add_to_cart, above_meta, below_meta, before_description_tab, after_description_tab, after_additional_information_tab, after_reviews_tab. Expand values: yes (link to expand) and no (always visible).

wsc_shipping_calculator_position_action_mapping

Filters the list of summary positions, each mapped to an action:priority pair. Add your own position here, then select it with wsc_setting_position:

add_filter( 'wsc_shipping_calculator_position_action_mapping', function( $mapping ) {
	$mapping['above_title'] = 'woocommerce_single_product_summary:4';

	return $mapping;
} );

add_filter( 'wsc_setting_position', function( $value ) {
	return 'above_title';
} );

wsc_shipping_calculator_position_action and wsc_shipping_calculator_position_priority

Filter the action and priority for the current summary position. See With a hook and priority.

wsc_is_shipping_calculator

Whether the current page shows the calculator and loads its scripts. By default, true on product pages for products that need shipping, and on pages whose content contains [shipping_calculator]. To hide the calculator on gift cards:

add_filter( 'wsc_is_shipping_calculator', function( $is_shipping_calculator ) {
	if ( is_product() && has_term( 'gift-cards', 'product_cat' ) ) {
		return false;
	}

	return $is_shipping_calculator;
} );

wsc_select_shipping_method

Return true to show a radio button next to each option when more than one is available, so shoppers can choose one in the calculator. Default false: the options are listed without a choice.

add_filter( 'wsc_select_shipping_method', '__return_true' );

wsc_load_using_ajax

Whether the calculator reloads itself after the page loads, so cached pages show current options. Default true, except on pages that define DONOTCACHEPAGE.

wsc_load_styles and wsc_load_scripts

Whether to load the calculator’s stylesheet and script. Both default to true. Return false from wsc_load_styles to style the calculator entirely with your own CSS. The calculator doesn’t work without its script.

add_filter( 'wsc_load_styles', '__return_false' );

wsc_script_params

Filters the settings passed to the calculator’s script (wsc_params).

add_filter( 'wsc_script_params', function( $params ) {
	// Change $params here.
	return $params;
} );

wsc_ajax_url_arg and wsc_is_shipping_calculator_ajax_call

wsc_ajax_url_arg filters the request argument (default wsc) that marks the calculator’s own AJAX requests. wsc_is_shipping_calculator_ajax_call filters whether the current request is one of them.

wsc_shipping_totals_class

Adds classes to the calculator’s wrapper element. Return a string of space-separated classes.

add_filter( 'wsc_shipping_totals_class', function( $classes ) {
	return 'my-shipping-calculator';
} );

wsc_shipping_calculator_shortcode_output

Filters the HTML the [shipping_calculator] shortcode returns.

woocommerce_get_settings_calculator

Filters the fields on WooCommerce » Settings » Shipping » Shipping calculator.

WooCommerce message filters

The calculator’s messages run through WooCommerce’s own filters, so a change applies to both the cart and the calculator: woocommerce_shipping_estimate_html, woocommerce_shipping_not_enabled_on_cart_html, woocommerce_no_shipping_available_html and woocommerce_cart_no_shipping_available_html (which also passes the formatted address).

Actions

wsc_shipping_totals_before_shipping

Fires inside the calculator’s table, before the shipping options. The plugin works out the estimate on this action at priority 10, so use a later priority to read it.

wsc_shipping_totals_after_shipping

Fires inside the calculator’s table, after the shipping options.

Template functions

  • wsc_display_shipping_calculator(): outputs the calculator for the current page.
  • wsc_get_setting( $setting_id ): returns a setting’s value, after its wsc_setting_{setting} filter. For example, wsc_get_setting( 'position' ).
  • wsc_load_using_ajax(): whether the calculator reloads itself after the page loads.

Templates

The calculator uses two templates, which you can copy to your theme to change:

  • wsc/shipping-totals.php: the calculator’s wrapper and table. Copy it to yourtheme/woocommerce/wsc/shipping-totals.php.
  • wsc/cart-shipping.php: the list of options and messages. Copy it to yourtheme/woocommerce/wsc/cart-shipping.php.

The address form is WooCommerce’s cart/shipping-calculator.php. To give the product page calculator its own version, put a copy at yourtheme/woocommerce/wsc/shipping-calculator.php.

Common issues

"Call to undefined function wsc_is_shipping_calculator()" after updating

Version 2.1.0 doesn’t define wsc_is_shipping_calculator() or wsc_is_shipping_calculator_ajax_call(). If your theme calls either of them, wrap each call in a function_exists() check, or call wsc_display_shipping_calculator() on its own: it already outputs nothing where the calculator can’t show.

My position filter doesn’t change anything

wsc_shipping_calculator_position_action and wsc_shipping_calculator_position_priority only apply to the product summary positions. For a tab, use wsc_setting_position; for any other spot, call wsc_display_shipping_calculator() from a template.

Keep learning