Shipping Simulator for WooCommerce Shortcode

Below, you’ll find a detailed guide on how to add the Shipping Simulator for WooCommerce Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Shipping Simulator for WooCommerce Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Shipping Simulator for WooCommerce Plugin and the shortcodes it provides:

Plugin Icon
Shipping Simulator for WooCommerce

"Shipping Simulator for WooCommerce is a useful plugin that allows you to simulate and manage shipping options. It enhances your WooCommerce store by offering flexible shipping calculations."

★★★★★ (18) Active Installs: 3000+ Tested with: 6.2.3 PHP Version: 7.4
Included Shortcodes:
  • [shipping_simulator]

Shipping Simulator for WooCommerce [shipping_simulator] Shortcode

The Shipping Simulator for WooCommerce shortcode is used to render a shipping simulator form on a product page. It fetches a product based on the product ID passed to it. If a product requires shipping, it triggers the ‘wc_shipping_simulator_shortcode_included’ action and enqueues necessary scripts. The form includes an input field for postcode, a submit button, and other customizable elements. Shortcode: [render_shortcode]

Shortcode: [shipping_simulator]

Parameters

Here is a list of all possible shipping_simulator shortcode parameters and attributes:

  • product – specifies the ID of the product for shipping simulation

Examples and Usage

Basic example – A simple usage of the Shipping Simulator for WooCommerce shortcode to specify a product by its ID.

[shipping_simulator product=25 /]

Advanced examples

Utilizing the shortcode without specifying a product. In this case, the global product object will be used if available. This is useful when you want to use the shortcode within a product’s description or another context where the product is already known.

[shipping_simulator /]

Using the shortcode with additional parameters. In this example, we are not only specifying the product by its ID but also changing the input placeholder, input type, input value, and submit label. This gives you more control over the appearance and functionality of the form.

[shipping_simulator product=25 input_placeholder="Enter your postcode" input_type="text" input_value="12345" submit_label="Calculate Shipping" /]

PHP Function Code

In case you have difficulties debugging what causing issues with [shipping_simulator] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( self::get_tag(), [ $this, 'render_shortcode' ] );

Shortcode PHP function:

function render_shortcode ( $atts ) {
		$atts = shortcode_atts( [
			'product' => 0,
		], $atts, self::get_tag() );

		$atts['product'] = absint( $atts['product'] );
		$product = null;
		if ( $atts['product'] !== 0 ) {
			$product = wc_get_product( $atts['product'] );
		} else {
			$product = $GLOBALS['product'] ?? null;
		}

		if ( is_object( $product ) && h::product_needs_shipping( $product ) ) {
			$this->product = $product;

			do_action( 'wc_shipping_simulator_shortcode_included', $atts );

			$this->enqueue_scripts();

			return h::get_template( 'shipping-simulator-form', [
				'css_class' => implode( ' ', apply_filters(
					'wc_shipping_simulator_wrapper_css_class',
					[]
				) ),
				// customizable template variables
				'input_placeholder' => apply_filters(
					'wc_shipping_simulator_form_input_placeholder',
					Settings::get_option( 'input_placeholder' )
				),
				'input_type' => apply_filters(
					'wc_shipping_simulator_form_input_type',
					'text'
				),
				'input_value' => apply_filters(
					'wc_shipping_simulator_form_input_value',
					$this->get_customer_postcode()
				),
				'submit_label' => apply_filters(
					'wc_shipping_simulator_form_submit_label',
					Settings::get_option( 'submit_label' )
				),
				'params' => apply_filters(
					'wc_shipping_simulator_form_js_params',
					$this->get_script_params()
				),
			] );
		}
		return '';
	}

Code file location:

shipping-simulator-for-woocommerce/shipping-simulator-for-woocommerce/classes/Shortcode.php

Conclusion

Now that you’ve learned how to embed the Shipping Simulator for WooCommerce Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *