Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.