Skip to content

Instantly share code, notes, and snippets.

@alordiel
Last active December 7, 2023 18:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alordiel/40e30a60ea070228e61f554a73ea96e9 to your computer and use it in GitHub Desktop.
Save alordiel/40e30a60ea070228e61f554a73ea96e9 to your computer and use it in GitHub Desktop.
Adding custom shipping method for WooCommerce (v. 3.2.6)
<?php
//Works with WooCommerce 3.2.6
add_action( 'woocommerce_shipping_init', 'econt_shipping_method' );
function econt_shipping_method() {
if ( ! class_exists( 'WC_Econt_Shipping_Method' ) ) {
class WC_Econt_Shipping_Method extends WC_Shipping_Method {
public function __construct( $instance_id = 0 ) {
$this->instance_id = absint( $instance_id );
$this->id = 'econt';//this is the id of our shipping method
$this->method_title = __( 'Econt Shipping', 'teame' );
$this->method_description = __( 'Delivery to office of Econt Express', 'teame' );
//add to shipping zones list
$this->supports = array(
'shipping-zones',
//'settings', //use this for separate settings page
'instance-settings',
'instance-settings-modal',
);
//make it always enabled
$this->title = __( 'Econt Shipping', 'teame' );
$this->enabled = 'yes';
$this->init();
}
function init() {
// Load the settings API
$this->init_form_fields();
$this->init_settings();
// Save settings in admin if you have any defined
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
}
//Fields for the settings page
function init_form_fields() {
//fileds for the modal form from the Zones window
$this->instance_form_fields = array(
'title' => array(
'title' => __( 'Title', 'teame' ),
'type' => 'text',
'description' => __( 'Title to be display on site', 'teame' ),
'default' => __( 'Delivery to office of Econt ', 'teame' )
),
'cost' => array(
'title' => __( 'Cost (lv)', 'teame' ),
'type' => 'number',
'description' => __( 'Cost of shipping', 'teame' ),
'default' => 4
),
);
//$this->form_fields - use this with the same array as above for setting fields for separate settings page
}
public function calculate_shipping( $package = array()) {
//as we are using instances for the cost and the title we need to take those values drom the instance_settings
$intance_settings = $this->instance_settings;
// Register the rate
$this->add_rate( array(
'id' => $this->id,
'label' => $intance_settings['title'],
'cost' => $intance_settings['cost'],
'package' => $package,
'taxes' => false,
)
);
}
}
}
//add your shipping method to WooCommers list of Shipping methods
add_filter( 'woocommerce_shipping_methods', 'add_econt_shipping_method' );
function add_econt_shipping_method( $methods ) {
$methods['econt'] = 'WC_Econt_Shipping_Method';
return $methods;
}
}
@rdbinteractive
Copy link

Thank you. I've been digging for hours.

It should not have been this hard to find working code.

Now let's hope they don't break this with an update.

I really appreciate you taking the time to post this.

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