Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save davegreenwp/6ef8b99f9342f91c58ef8d274947d688 to your computer and use it in GitHub Desktop.
Save davegreenwp/6ef8b99f9342f91c58ef8d274947d688 to your computer and use it in GitHub Desktop.
Example instance based shipping method
<?php
/**
* Example of a custom WC_Shipping_Method class.
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class WC_Shipping_Test_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', 'textdomain' );
$this->method_description = __( 'Some shipping method.', 'textdomain' );
// Apply this fee to all instances, on top of the cost. Can be a percentage.
$this->fee = '0';
// Set a minimum value for the fee; only useful with percentage based fees.
$this->minimum_fee = '0';
$this->enabled = $this->get_option( 'enabled' );
$this->title = $this->get_option( 'title' );
$this->supports = array(
'shipping-zones',
'instance-settings',
'instance-settings-modal'
);
$this->instance_form_fields = array(
'enabled' => array(
'title' => __( 'Enable/Disable', 'textdomain' ),
'type' => 'checkbox',
'label' => __( 'Enable this shipping method', 'textdomain' ),
'default' => 'yes',
),
'title' => array(
'title' => __( 'Method Title', 'textdomain' ),
'type' => 'text',
'description' => __( 'This controls the title the customer sees during checkout.', 'textdomain' ),
'default' => __( 'Test Method', 'textdomain' ),
'desc_tip' => true
),
'cost' => array(
'title' => __( 'Cost', 'textdomain' ),
'type' => 'text',
'placeholder' => '',
'description' => __( 'This controls the cost that is applied for this shipping method at checkout.', 'textdomain' ),
'default' => '100.00',
'desc_tip' => true,
'sanitize_callback' => 'wc_format_decimal',
),
);
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
}
/**
* Calculate shipping for this method.
*
* @param array $package (default: array())
*/
public function calculate_shipping( $package = array() ) {
// In this example, we're applying a single cost for the method,
// but if you are using `per_item` tax caculation, you will need
// to multiply the cost by the number of items in $package and
// pass that figure in as the total instead.
$total = wc_format_decimal( $this->get_option( 'cost' ) );
$this->add_rate(
array(
'id' => $this->id . ':' . $this->instance_id,
'label' => $this->title,
'cost' => $total + $this->get_fee( $this->fee, $total ),
'calc_tax' => 'per_order', // Or `per_item` to calculate for each item.
)
);
}
}
<?php
/**
* Load our test method class when WC initialises shipping.
*
* @return void
*/
function init_wc_shipping_test_method() {
if ( ! class_exists( 'WC_Shipping_Test_Method' ) ) {
// Alter this accordingly to correctly include the class
include 'class-wc-shipping-test-method.php';
}
}
add_action( 'woocommerce_shipping_init', 'init_wc_shipping_test_method' );
/**
* Add our test method to the list of available methods.
*
* @param array $methods Methods as method_id => class name.
* @return void
*/
function add_wc_shipping_test_method( $methods ) {
$methods['test_method'] = 'WC_Shipping_Test_Method';
return $methods;
}
add_filter( 'woocommerce_shipping_methods', 'add_wc_shipping_test_method' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment