Custom Shipping Methods For Woocommerce Shortcode

Below, you’ll find a detailed guide on how to add the Custom Shipping Methods 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 Custom Shipping Methods For Woocommerce Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Custom Shipping Methods For Woocommerce Plugin and the shortcodes it provides:

Plugin Icon
Custom Shipping Methods for WooCommerce – Create Weight based Shipping, Conditional Shipping, Table Rate Shipping and much more

"Custom Shipping Methods for WooCommerce is a versatile plugin that offers features like weight-based, conditional, and table rate shipping options, providing you with more control over your shipping methods."

★★★★☆ (15) Active Installs: 3000+ Tested with: 6.2.3 PHP Version: 5.6
Included Shortcodes:
  • [fee]

Custom Shipping Methods For Woocommerce [fee] Shortcode

The Custom Shipping Methods for WooCommerce plugin shortcode ‘fee’ calculates shipping fees based on pre-set parameters. It calculates the fee as a percentage of the total cost. The shortcode allows setting a minimum and maximum fee limit. If the calculated fee is less than the minimum or exceeds the maximum, the shortcode adjusts it accordingly.

Shortcode: [fee]

Parameters

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

  • percent – The percentage of the fee cost to be calculated.
  • min_fee – The minimum limit for the calculated fee.
  • max_fee – The maximum limit for the calculated fee.

Examples and Usage

Basic example – A simple usage of the ‘fee’ shortcode to calculate a fee based on a percentage of the cost.

[fee percent=10 /]

The above example calculates a fee that is 10% of the cost. If the cost is $100, the calculated fee would be $10.

Advanced examples

Utilizing the ‘fee’ shortcode to calculate a fee based on a percentage of the cost, with a minimum and maximum fee limit.

[fee percent=10 min_fee=5 max_fee=50 /]

In this advanced example, the fee is calculated as 10% of the cost, but it also includes a minimum and maximum limit. If the calculated fee is less than $5, the fee will be set to $5. If the calculated fee is more than $50, the fee will be set to $50. This ensures the fee stays within a specified range regardless of the cost.

Another advanced usage of the ‘fee’ shortcode where only a minimum fee limit is set, without a percentage or maximum fee limit.

[fee min_fee=5 /]

This example sets a minimum fee of $5. This means regardless of the cost, the fee will never be less than $5. As there is no percentage or maximum fee set, the fee will be $5 regardless of the cost.

PHP Function Code

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

Shortcode line:

add_shortcode( 'fee', array( $this, 'fee' ) );

Shortcode PHP function:

                    function fee( $atts ) {
			$atts = shortcode_atts(
				array(
					'percent' => '',
					'min_fee' => '',
					'max_fee' => '',
				),
				$atts,
				'fee'
			);

			$calculated_fee = 0;

			if ( $atts['percent'] ) {
				$calculated_fee = $this->fee_cost * ( floatval( $atts['percent'] ) / 100 );
			}

			if ( $atts['min_fee'] && $calculated_fee < $atts['min_fee'] ) {
				$calculated_fee = $atts['min_fee'];
			}

			if ( $atts['max_fee'] && $calculated_fee > $atts['max_fee'] ) {
				$calculated_fee = $atts['max_fee'];
			}

			return $calculated_fee;
		}
                    

Code file location:

custom-shipping-methods-for-woocommerce/custom-shipping-methods-for-woocommerce/includes/class-wc-shipping-alg-custom.php

Conclusion

Now that you’ve learned how to embed the Custom Shipping Methods 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 *