By default WooCommerce Advanced Shipping will fit many needs. With its conditional logic a countless amount of combinations of shipping rates can be setup. There are however still cases where those default conditions will not fit all needs. WooCommerce Advanced Shipping has been created to be easily extendable and thus conditions can be added easily. There are a couple custom scripts available so you can get in touch via https://jeroensormani.com/woocommerce-advanced-shipping/support/ for information.
Adding a custom condition
Adding a new custom conditions consists out of 3 required functions, and 1 optional.
1) Adding the condition
The first thing you need to do is add a new condition in the drop down list. In the code snippet below we’re adding a ‘Date’ condition to the drop down.
<?php | |
/** | |
* Add condition to conditions list. | |
* | |
* @param array $conditions List of existing conditions. | |
* @return aray List of modified conditions. | |
*/ | |
function was_conditions_add_date( $conditions ) { | |
// 'General', 'User Details', 'Cart' are default groups, you can also use something custom | |
$conditions['General']['date'] = __( 'Date', 'woocommerce-advanced-shipping' ); | |
return $conditions; | |
} | |
add_filter( 'was_conditions', 'was_conditions_add_date', 10, 1 ); |
2) Adding the value field
There are two possible value fields, 1) a regular text field, 2) a drop down field. In the code snippet below a regular text field is used, but a example for a drop down is also included in a commented out section.
<?php | |
/** | |
* Add value field for 'date' condition | |
* | |
* @param array $values List of value field arguments | |
* @param string $condition Name of the condition. | |
* @return array $values List of modified value field arguments. | |
*/ | |
function was_values_add_date( $values, $condition ) { | |
switch ( $condition ) { | |
case 'date': | |
$values['field'] = 'text'; | |
$values['placeholder'] = 'dd-mm-yyyy or yyyy-mm-dd'; | |
// Option 2; Drop down value field | |
// | |
// $values['field'] = 'select'; | |
// | |
// foreach ( array( '1', '2' ) as $key => $value ) : | |
// $values['options'][ $key ] = $value; | |
// endforeach; | |
break; | |
} | |
return $values; | |
} | |
add_filter( 'was_values', 'was_values_add_date', 10, 2 ); |
3) Adding the matching condition
In this step we’re adding the functionality to see if the customer matches the variable the store owner has setup. In the code snippet below you can see the matching functions in action. This particular function can be compared to all operators (some condition only require the ‘equal to’ and ‘not equal to’ operators).
<?php | |
/** | |
* Must match given date. | |
* | |
* @param bool $match Current matching status. Default false. | |
* @param string $operator Store owner selected operator. | |
* @param mixed $value Store owner given value. | |
* @param array $package Shipping package. | |
* @return bool If the current user/environment matches this condition. | |
*/ | |
function was_match_condition_date( $match, $operator, $value, $package ) { | |
// Check if its a date, when false subtract the number of days | |
if ( date( 'Y-m-d', strtotime( $value ) ) > 1970 ) : | |
if ( $operator == '==' ) : | |
$match = ( current_time( 'Y-m-d' ) == date_i18n( 'Y-m-d', strtotime( $value ) ) ); | |
elseif ( $operator == '!=' ) : | |
$match = ( current_time( 'Y-m-d' ) != date_i18n( 'Y-m-d', strtotime( $value ) ) ); | |
elseif ( $operator == '>=' ) : | |
$match = ( current_time( 'Y-m-d' ) >= date_i18n( 'Y-m-d', strtotime( $value ) ) ); | |
elseif ( $operator == '<=' ) : | |
$match = ( current_time( 'Y-m-d' ) <= date_i18n( 'Y-m-d', strtotime( $value ) ) ); | |
endif; | |
endif; | |
return $match; | |
} | |
add_action( 'was_match_condition_date', 'was_match_condition_date', 10, 4 ); |
4) Adding the condition description (optional)
Via the code snippet below you can add a description that will show up when someone hovers over the question mark on the right side of the condition. When not used the question mark will not show up.
<?php | |
/** | |
* Add date description. | |
* | |
* @param array $description List of existing descriptions. | |
* @return array List of modified descriptions. | |
*/ | |
function was_descriptions_date( $descriptions ) { | |
$descriptions['date'] = __( 'Compare to the current date.', 'woocommerce-advanced-shipping' ); | |
return $descriptions; | |
} | |
add_filter( 'was_descriptions', 'was_descriptions_date' ); |
Custom condition done for you
It could be that you cannot figure things out or that you’re simply not a developer. If you want me to custom code your custom condition you can hire me. For a normal (meaning no hugely weird/complicated stuff) custom condition I ask €150,-. If you’re interested, you can reach out to me via the following contact form http://jeroensormani.com/woocommerce-advanced-shipping/support/, please mention that you’re interested in a custom condition.