Filter uncanny-automator-pro

automator_woocommerce_get_shipping_methods

Filters shipping methods before they are retrieved for a WooCommerce order.

add_filter( 'automator_woocommerce_get_shipping_methods', $callback, 10, 2 );

Description

This filter hook allows developers to modify the list of available WooCommerce shipping methods before they are displayed in Automator. Use it to add, remove, or rename shipping methods for custom integrations or to control which options are presented to users creating automations.


Usage

add_filter( 'automator_woocommerce_get_shipping_methods', 'your_function_name', 10, 2 );

Parameters

$methods (mixed)
This parameter holds an array of available shipping methods for a WooCommerce order.
$this (mixed)
This parameter is a mixed array that initially holds available shipping methods and is used to add, remove, or modify shipping method options before they are displayed.

Return Value

The filtered value.


Examples

<?php
/**
 * Example of how to filter the shipping methods available in Uncanny Automator for WooCommerce.
 * This example will add a custom shipping method if it doesn't already exist.
 */
add_filter(
	'automator_woocommerce_get_shipping_methods',
	function ( $methods, $object ) {
		// Check if a custom shipping method 'my_custom_shipping' already exists.
		if ( ! array_key_exists( 'my_custom_shipping', $methods ) ) {
			// Add our custom shipping method to the array.
			$methods['my_custom_shipping'] = __( 'My Custom Shipping Method', 'your-text-domain' );

			// Optionally, you might also need to update the internal details if the object
			// is used elsewhere to retrieve method details. The exact structure of $object
			// would depend on the Uncanny Automator Pro codebase. Assuming it has a similar
			// structure to the source context for demonstration.
			if ( isset( $object->shipping_method_details ) && is_array( $object->shipping_method_details ) ) {
				$object->shipping_method_details['my_custom_shipping'] = array(
					'title' => __( 'My Custom Shipping Method', 'your-text-domain' ),
					'id'    => 'my_custom_shipping',
					'cost'  => 15.00, // Example cost
				);
			}
		}

		// Always return the modified methods array.
		return $methods;
	},
	10, // Priority: 10 is the default, meaning it runs after most other filters.
	2  // Accepted arguments: $methods and $object.
);

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

uncanny-automator-pro/src/integrations/woocommerce/helpers/woocommerce-pro-helpers.php:498
uncanny-automator-pro/src/integrations/woocommerce-subscription/fields/order-fields.php:1003

public function get_shipping_methods() {

		$methods = array();

		$methods['-1']                    = esc_html_x( 'Select shipping method', 'WooCommerce', 'uncanny-automator-pro' );
		$methods['0']                     = esc_html_x( 'Manual', 'WooCommerce', 'uncanny-automator-pro' );
		$this->shipping_method_details[0] = array(
			'title' => esc_html_x( 'Manual', 'WooCommerce', 'uncanny-automator-pro' ),
			'id'    => 0,
		);
		foreach ( $this->get_shipping_zones() as $zone ) {
			$zone_name = $zone['zone_name'];
			foreach ( $zone['shipping_methods'] as $method ) {
				if ( 'yes' !== $method->enabled ) {
					continue;
				}
				$instance_id             = $method->instance_id;
				$method_title            = $method->method_title;
				$title                   = $method->title;
				$methods[ $instance_id ] = sprintf( '%s - %s (%s)', $zone_name, $title, $method_title );

				$this->shipping_method_details[ $instance_id ] = array(
					'title' => $title,
					'id'    => $method->id,
					'cost'  => isset( $method->cost ) ? $method->cost : 0,
				);
			}
		}
		$methods['free_shipping']                       = esc_html_x( 'Free shipping', 'WooCommerce', 'uncanny-automator-pro' );
		$this->shipping_method_details['free_shipping'] = array(
			'title' => esc_html_x( 'Free shipping', 'WooCommerce', 'uncanny-automator-pro' ),
			'id'    => 'free_shipping',
			'cost'  => 0,
		);
		$methods['flat_rate']                           = esc_html_x( 'Flat rate', 'WooCommerce', 'uncanny-automator-pro' );
		$this->shipping_method_details['flat_rate']     = array(
			'title' => esc_html_x( 'Flat rate', 'WooCommerce', 'uncanny-automator-pro' ),
			'id'    => 'flat_rate',
			'cost'  => 0,
		);

		return apply_filters( 'automator_woocommerce_get_shipping_methods', $methods, $this );
	}

Scroll to Top