Created
November 21, 2019 00:18
-
-
Save MogulChris/cb04cd8ce3b42a1b3fb6a377a40a5089 to your computer and use it in GitHub Desktop.
WooCommerce custom shipping method plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//I found a few tutorials for making custom WooCommerce shipping methods but most were confusing or didn't work any more. | |
//This is a simple custom shipping method. | |
//It originally calculated a fee per 'delivery day' based on some date meta on the cart items, but for this example it's a hard-coded flat fee. | |
//See calculate_shipping() | |
//Step 1: | |
//Save this code (minus these instructions) in a file in your plugins directory, preferably in its own subdirectory eg myplugin_shipping/myplugin_shipping.php | |
//Step 2: | |
//Find and replace the following strings (case sensitive, use the same patterns) with variations of your plugin name | |
/* | |
myplugin_name - used for various hook and method names | |
WC_Myplugin_Delivery_Method - shipping class name | |
My Plugin - text shown to end user in labels | |
*/ | |
//Step 3: | |
//Enable the plugin, then enable the shipping method in WC settings (it should have its own settings tab) | |
//Step 4: | |
//Cross fingers and test | |
?> | |
<?php | |
/** | |
* Plugin Name: My Plugin Delivery Shipping method | |
* Description: Custom shipping method plugin for WooCommerce | |
*/ | |
if ( ! defined( 'WPINC' ) ){ | |
die('security by preventing any direct access to your plugin file'); | |
} | |
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { | |
function myplugin_name_delivery_init() { | |
if ( ! class_exists( 'WC_Myplugin_Delivery_Method' ) ) { | |
class WC_Myplugin_Delivery_Method extends WC_Shipping_Method { | |
/** | |
* Constructor for your shipping class | |
* | |
* @access public | |
* @return void | |
*/ | |
public function __construct($instance_id = 0) { | |
$this->id = 'myplugin_name_delivery'; // Id for your shipping method. Should be uunique. | |
$this->instance_id = absint( $instance_id ); | |
$this->method_title = __( 'My Plugin Delivery' ); // Title shown in admin | |
$this->method_description = __( 'Flat delivery fee per day' ); // Description shown in admin | |
$this->init(); | |
$this->title = !empty($this->settings['title']) ? $this->settings['title'] : 'My Plugin Delivery'; // This can be added as an setting but for this example its forced. | |
//$this->enabled = isset($this->settings['enabled']) ? $this->settings['enabled'] : 'yes'; | |
$this->flat_fee = isset($this->settings['flat_fee']) ? $this->settings['flat_fee'] : '4.00'; | |
} | |
/** | |
* 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' ) ); | |
} | |
function init_form_fields() { | |
$this->form_fields = array( | |
'enabled' => array( | |
'title' => __( 'Enable', 'dc_raq' ), | |
'type' => 'checkbox', | |
'description' => __( 'Enable this shipping method.', 'dc_raq' ), | |
'default' => 'yes' | |
), | |
'title' => array( | |
'title' => __( 'Title', 'dc_raq' ), | |
'type' => 'text', | |
'description' => __( 'Title to be displayed on site', 'dc_raq' ), | |
'default' => __( 'Request a Quote', 'dc_raq' ) | |
), | |
'flat_fee' => array( | |
'title' => __( 'Flat fee per day', 'dc_raq' ), | |
'type' => 'text', | |
'description' => __( 'Delivery fee per day', 'dc_raq' ), | |
'default' => '4' | |
), | |
); | |
} | |
/** | |
* calculate_shipping function. | |
* | |
* @access public | |
* | |
* @param mixed $package | |
* | |
* @return void | |
*/ | |
public function calculate_shipping( $package = array() ) { | |
//Most carts will contain only one $package but sometimes there are more eg WooCommerce Subscriptions has a separate package for its recurring cart | |
//That's why we will look at $package only, not WC()->cart, to calculate shipping | |
//Calculate your shipping fee here. $package['contents'] is the same structure as a WC cart | |
/* | |
foreach($package['contents'] as $item_id => $item){ | |
//$_product = $item['data']; | |
//etc | |
} | |
*/ | |
$fee = 1.99; //for the demo, just use a flat $1.99 | |
//Add the fee you calculated | |
$rate = array( | |
'id' => $this->id, | |
'label' => $this->title, | |
'cost' => $fee, | |
'calc_tax' => 'per_item', | |
'package' => $package | |
); | |
// Register the rate | |
$this->add_rate( $rate ); | |
}//calculate_shipping() | |
} | |
} | |
} | |
add_action( 'woocommerce_shipping_init', 'myplugin_name_delivery_init' ); | |
function myplugin_name_delivery_shipping_method( $methods ) { | |
$methods['myplugin_name_delivery'] = 'WC_Myplugin_Delivery_Method'; | |
return $methods; | |
} | |
add_filter( 'woocommerce_shipping_methods', 'myplugin_name_delivery_shipping_method' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment