-
-
Save mikejolley/6713608 to your computer and use it in GitHub Desktop.
<?php | |
/* | |
Plugin Name: Your Shipping plugin | |
Plugin URI: http://woothemes.com/woocommerce | |
Description: Your shipping method plugin | |
Version: 1.0.0 | |
Author: WooThemes | |
Author URI: http://woothemes.com | |
*/ | |
/** | |
* Check if WooCommerce is active | |
*/ | |
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { | |
function your_shipping_method_init() { | |
if ( ! class_exists( 'WC_Your_Shipping_Method' ) ) { | |
class WC_Your_Shipping_Method extends WC_Shipping_Method { | |
/** | |
* Constructor for your shipping class | |
* | |
* @access public | |
* @return void | |
*/ | |
public function __construct() { | |
$this->id = 'your_shipping_method'; // Id for your shipping method. Should be uunique. | |
$this->method_title = __( 'Your Shipping Method' ); // Title shown in admin | |
$this->method_description = __( 'Description of your shipping method' ); // Description shown in admin | |
$this->enabled = "yes"; // This can be added as an setting but for this example its forced enabled | |
$this->title = "My Shipping Method"; // This can be added as an setting but for this example its forced. | |
$this->init(); | |
} | |
/** | |
* Init your settings | |
* | |
* @access public | |
* @return void | |
*/ | |
function init() { | |
// Load the settings API | |
$this->init_form_fields(); // This is part of the settings API. Override the method to add your own settings | |
$this->init_settings(); // This is part of the settings API. Loads settings you previously init. | |
// Save settings in admin if you have any defined | |
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) ); | |
} | |
/** | |
* calculate_shipping function. | |
* | |
* @access public | |
* @param mixed $package | |
* @return void | |
*/ | |
public function calculate_shipping( $package ) { | |
$rate = array( | |
'id' => $this->id, | |
'label' => $this->title, | |
'cost' => '10.99', | |
'calc_tax' => 'per_item' | |
); | |
// Register the rate | |
$this->add_rate( $rate ); | |
} | |
} | |
} | |
} | |
add_action( 'woocommerce_shipping_init', 'your_shipping_method_init' ); | |
function add_your_shipping_method( $methods ) { | |
$methods['your_shipping_method'] = 'WC_Your_Shipping_Method'; | |
return $methods; | |
} | |
add_filter( 'woocommerce_shipping_methods', 'add_your_shipping_method' ); | |
} |
Uhm.. tried to use this plugin but no options was available? The method appears correct, but when i go to change the settings (i.e. price of method) its empty...
Please update this example for WooCommerce 2.6.0
Check out my fork:
https://gist.github.com/rpocc/06f63d9383b5e742705e921e8e46f193
It it compatible with WooCommerce 2.6.0
Some Shipping Class-related code was just copied from Flat Rate shipping method, but initialization of the class is also different.
- Initialization functions should be inside a class
- Nested classes are not supported by php and because of that you need to use separate files
- shipping method is now being kept not in a simple indexed array, but in keyed array.
I'm a third-party developer and can't explain everything I've done, but empirically I've managed to make my own plug-in work using very similar code. I hope my additions will help to update almost any good shipping method plugin before official updated example will appear.
Dmitry Shtatnov.
Hi @mikejolley, there are a few changes that have to be made to this code to work. Could you please update whenever you have the time. Here are the updates that need to be done:
-
There should be an argument passed to the constructor like
__construct( $instance_id = 0 )
and then in the body of the constructor:$this->instance_id = absint( $instance_id );
-
Also the
calculate_shipping
method should be with predefined argument as so:public function calculate_shipping( $package = array())
-
If you would like to have option to edit your shipping method from a single settings page, or to see it in the options with the other shipping methods in the shipping zone page you will need to add in the constructor the following code:
$this->supports = array(
'shipping-zones',//make your shipping method available for the shipping zones
'settings', //use this for separate settings page
'instance-settings', //add options for to edit the settings of your shipping method for a single zone
'instance-settings-modal',
);
For everyone interest in full working demo, please check here.
I was wondering how to get a shipping method to just appear under zones under the shipping zones section and not as a separate section
The reason I ask is because this shows up in my list of shipping options
And I'd only like the shipping zones options to appear not the UK Flat Rate as well?
Is that possible?
Many thanks, Alordiel.
Genius! Saved my bacon today.