skip to Main Content

Adding a New Condition

The Table Rate shipping method for WooCommerce comes with many conditional values, but sometimes your shipping model requires something unique. This is where the extensive list of filters and action hooks come in handy. With a little PHP knowledge, you can use these filters to create a new condition that will work seamlessly alongside the default settings. There are three filters we will focus on in this example:

  • betrs_shipping_cost_conditionals
    Adds the new option to the main select box under the Conditions column
  • betrs_shipping_cost_conditionals_secondary
    Chooses which secondary statements apply to the new condition
  • betrs_determine_condition_result
    Completes the actual function that will determine true or false when this condition is being used

Dimensional Sum Example

The shop owner in this scenario needs to calculate the sum of a product’s dimensions (width + height + length). This is not a built in feature of the Table Rate method, but it can be added using the three filters listed above. Below is the full source code for adding this ability. Beneath that is the description to further explain how this code operates.

The first function is the simplest. It simply takes in the list of conditions predefined by the Table Rate method, and adds a new one to the end. The ID for this option is ‘dimensions_sum’ and it must be unique from the other options. The title is simply listed as ‘Dimensions Sum’.

The second function determines what options are populated into the condition’s secondary select box. The dimensional sum will always return a number value just like volume, area, and other number based options. So here we can use the greater than, less than, or equal to statements.

These three conditional statements are already defined in this method, however it is simpler to do the calculations manually rather than altering more settings variables. This is the purpose of the third function in this tutorial. It will calculate the sum of all dimensions as requested, then perform the appropriate comparison

Once the code is properly added to your site, the end result will show up in the Conditions column of the Table Rates section. In this example, the shop owner requires that the sum of the product’s dimensions be 90cm or less in order to qualify for the $9.99 shipping cost.

new condition dimensions sum

Longest Length Example

In some cases, the pre-calculated cart data may not cover everything you need. In this example, the shop owner needs to know the length of the longest product in the cart. By default, this shipping method would return the combined total length of all products in the cart. This won’t help the owner find the longest length, so instead we must add this to the calculated cart data before adding a new condition.

Just as with the previous example, the first two functions simply populate the select box with the new condition’s information. The third function is used to calculate the longest length and add it to the variable holding the calculated values for the cart data. The fourth and final function will then use that new data to compare and see if it is greater than, less than, or equal to.

Calculating Per Class

The examples above are great if you are calculating shipping based on the order as a whole. If you have changed the Base Table Rules option to something other than Per Order, your code will be different. Let’s calculate the longest length for Per Class setups as well! In addition to the code above, you will need to add a new function that calculates the longest length for each shipping class group. The data array is a little different for this one since we are grouping the items in the cart, so it requires its own function.

  • Was this article helpful ?
  • yesno
1 out of 1 visitors found this article helpful
Views: 333
Back To Top