Skip to content

Instantly share code, notes, and snippets.

@mikejolley
Last active October 25, 2024 13:38
Show Gist options
  • Save mikejolley/3b37b9cc19a774665f31 to your computer and use it in GitHub Desktop.
Save mikejolley/3b37b9cc19a774665f31 to your computer and use it in GitHub Desktop.
Example instance based shipping method
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Sample instance based method.
*/
class WC_Shipping_Test_Method extends WC_Shipping_Method {
/**
* Constructor. The instance ID is passed to this.
*/
public function __construct( $instance_id = 0 ) {
$this->id = 'test_method';
$this->instance_id = absint( $instance_id );
$this->method_title = __( 'Test Method' );
$this->method_description = __( 'Some shipping method.' );
$this->supports = array(
'shipping-zones',
'instance-settings',
);
$this->instance_form_fields = array(
'enabled' => array(
'title' => __( 'Enable/Disable' ),
'type' => 'checkbox',
'label' => __( 'Enable this shipping method' ),
'default' => 'yes',
),
'title' => array(
'title' => __( 'Method Title' ),
'type' => 'text',
'description' => __( 'This controls the title which the user sees during checkout.' ),
'default' => __( 'Test Method' ),
'desc_tip' => true
)
);
$this->enabled = $this->get_option( 'enabled' );
$this->title = $this->get_option( 'title' );
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
}
/**
* calculate_shipping function.
* @param array $package (default: array())
*/
public function calculate_shipping( $package = array() ) {
$this->add_rate( array(
'id' => $this->id . $this->instance_id,
'label' => $this->title,
'cost' => 100,
) );
}
}
@owlyowl
Copy link

owlyowl commented Apr 28, 2018

Is there a way to get rid of the backward compatibility so a custom shipping method plugin doesn't have a settings page?
I'm trying to only show instances of a given shipping method based on the zones they are in? i.e. if a method isn't in a zone don't show the default instance from a settings page.

This is my setting in the constructor yet it still shows UK Flat rate in the menu and in the checkout page it is showing UK FLAT RATE as well as the ones defined in my UK zone whereas I would have expected there to be no UK FLAT RATE radio button.

$this->supports = array( 'shipping-zones', 'instance-settings', 'instance-settings-modal', );

uk1

@rangatia
Copy link

Is there a way to get rid of the backward compatibility so a custom shipping method plugin doesn't have a settings page?
I'm trying to only show instances of a given shipping method based on the zones they are in? i.e. if a method isn't in a zone don't show the default instance from a settings page.

This is my setting in the constructor yet it still shows UK Flat rate in the menu and in the checkout page it is showing UK FLAT RATE as well as the ones defined in my UK zone whereas I would have expected there to be no UK FLAT RATE radio button.

$this->supports = array( 'shipping-zones', 'instance-settings', 'instance-settings-modal', );

uk1

stumbled upon this old post, and faced this issue ...

(think for backward compatibility reasons) WC_Shipping_Method addes 'settings' to the 'supports' property by default.
You can reset this to empty array (or with other features that your custom shiping method supports) in your extended class to overwrite 'settings' (and removed as tab from Woo > Settings > Shipping ).

...
    class Your_Shipping_Method extends WC_Shipping_Method {
    ...
        public function __construct() {
        ...
        $this->supports = []; // overwrites the default support for 'settings' feature
        ...
        }
    ...
    }
...

tested with WC4.0 & WP5.3

@erikdemarco
Copy link

@mikejolley Can you update this gist with example how to restrict this shipping method only to certain country. In non-instance based is easy, just set the countries and availability, but how its done in instance based?

@davegreenwp
Copy link

davegreenwp commented Apr 21, 2023

@mikejolley this snippet is incredibly useful as a reference, thanks! 🙌

I think there's value adding the cost field as an example here too?

'cost' => array(
	'title' => __( 'Cost' ),
	'type' => 'text',
	'placeholder' => '',
	'description' => __( 'This controls the cost that is applied for this shipping method at checkout.' ),
	'default' => '0',
	'desc_tip' => true,
	'sanitize_callback' => '',
),

One final suggestion is to add instance-settings-modal to the supports array, as this is a much nicer way to edit the method settings:

$this->supports = array(
    'shipping-zones',
    'instance-settings',
    'instance-settings-modal'
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment