Hooks (action & filter)

Hooks (action & filter)

Hooks in WordPress essentially allow you to change or add code without editing core files. They are used extensively throughout WordPress and WooCommerce and are very useful for developers.

Read more about action and filter hooks here:


How and where to insert the code?


To get the functions exemplified in this section to work you need to add the code to your theme’s functions.php. You can add it as its own plugin or through the Code Snippets plugin, or something similiar of your choice.


Filters


Allow free orders in Avarda Checkout

As a WooCommerce default, when an order has 0 value (free trial, applied coupons…), Avarda Checkout is not loaded. Instead the standard WooCommerce checkout is loaded. If you have Avarda as the only payment method this means that the customer can’t complete the order.

With the filter below you allow free orders with Avarda Checkout.

/**
 * Allow free orders using Avarda Checkout for WooCommerce.
 *
 * @return bool
 */
function aco_allow_free_orders() {
	return true;
}

add_filter( 'woocommerce_order_needs_payment', 'aco_allow_free_orders', 999 );
add_filter( 'woocommerce_cart_needs_payment', 'aco_allow_free_orders', 999 );

Set shipping method carrier

If you are displaying shipping in the embedded Avarda Checkout and want to filter the shipping method carrier to be set for the shipping rate in the Avarda shipping module, you can use the filter below.

By changing the $carrier value (in this example “my_carrier_id”) you can set a shipping method carrier of your choosing.

/**
 * Filter the shipping method carrier to be set for the shipping rate in the Avarda shipping module. This can be used to set the carrier for a shipping rate to use the included carrier icons.
 * Refer to the ACO_Shipping_Option_Model::get_shipping_method_icon method here to see the available default carriers: https://github.com/krokedil/avarda-checkout-for-woocommerce/blob/master/classes/api/models/shipping/class-aco-shipping-option-model.php
 * You can also filter the icon if needed if you want to set your own carrier id and icon.
 * 
 * If no carrier is set from the filter the plugin will look at the rate and try to get the carrier from the metadata keys 'carrier' or 'udc_carrier_id'. 
 * Which can also be used instead of the filter when the shipping rate is calculated if needed.
 * 
 * @param string $carrier The carrier to be set for the shipping rate.
 * @param WC_Shipping_Rate $shipping_rate The shipping rate to set the carrier for from WooCommerce
 * 
 * @return string The carrier to be set for the shipping rate.
 */
function custom_aco_shipping_method_carrier( $carrier, $shipping_rate ) {
	if( $shipping_rate->get_method_id() === 'my_shipping_id' ) {
		$carrier = 'my_carrier_id';
	}
	
	return $carrier;
}
add_filter( 'aco_shipping_method_carrier', 'custom_aco_shipping_method_carrier', 10, 2 );

Set description for shipping method

If you are displaying shipping in the embedded Avarda Checkout and want to set your own shipping method description you can use the filter below.

By changing the $description value (in this example “My filtered description”) you can set a description of your choosing.

/**
 * Add a custom description to your shipping method in the Avarda Checkout.
 * This will be displayed in the shipping method when its listed by the checkout.
 * 
 * This will override the setting that the plugin adds to shipping rates.
 * 
 * @param string $description The description to be set for the shipping rate.
 * @param WC_Shipping_Rate $shipping_rate The shipping rate to set the description for from WooCommerce
 * 
 * @return string The description to be set for the shipping rate.
 */
function custom_aco_shipping_method_description( $description, $shipping_rate ) {
	// If the shipping method id is my_shipping_id, set the description to a custom description.
	if( $shipping_rate->get_method_id() === 'my_shipping_id' ) {
		$description = 'My custom description';
	}
	
	return $description;
}
add_filter( 'aco_shipping_method_description', 'custom_aco_shipping_method_description', 10, 2 );

Set icon for shipping method

If you are displaying shipping in the embedded Avarda Checkout and want to set an icon for the shipping method you can use the filter below.

By changing the $img_url value (in this example “https://example.com/my-icon.png”) you can set an icon of your choosing.

/**
 * Filter the shipping method icon url based on the carrier id or some data from the shipping rate.
 * This can be used to set your own icons for any carrier, even the once we add default icons for. Or for any custom logic on the shipping rate itself if needed.
 * 
 * This will override the setting that the plugin adds to shipping rates.
 * 
 * @param string $img_url The icon url to be set for the shipping rate.
 * @param string $carrier The carrier to be set for the shipping rate.
 * @param WC_Shipping_Rate $shipping_rate The shipping rate to set the icon for from WooCommerce
 * 
 * @return string The icon url to be set for the shipping rate.
 */
function custom_aco_shipping_method_icon( $img_url, $carrier, $shipping_rate ) {
	// If the carrier is set to my_carrier_id or the shipping method id is my_shipping_id, set the icon to a custom icon.
	if( $shipping_rate->get_method_id() === 'my_shipping_id' || $carrier === 'my_carrier_id' ) {
		$img_url = 'https://example.com/my-icon.png';
	}
	
	return $img_url;
}
add_filter( 'aco_shipping_icon', 'custom_aco_shipping_method_icon', 10, 3 );