Skip to content

Instantly share code, notes, and snippets.

@jbokhari
Created November 12, 2015 20:31
Show Gist options
  • Select an option

  • Save jbokhari/e3ea88fdf5be91d4383d to your computer and use it in GitHub Desktop.

Select an option

Save jbokhari/e3ea88fdf5be91d4383d to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Custom Shipping Methods
Plugin URI: http://woothemes.com/woocommerce
Description: Adds hard-coded custom shipping methods. Adds: Express Mail Flat Rate | FedEx Ground (Domestic Only) | Fedex Overnight Domestic | International FedEx | International Shipping USPS.
Version: 1.0.0
Author: Jameel Bokhari
Author URI: http://www.anchorwave.com
*/
# link
// Dis plugin mostly from @link https://docs.woothemes.com/document/shipping-method-api/
/**
* Check if WooCommerce is active
*/
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
function your_shipping_method_init() {
class WC_Express_Shipping extends WC_Shipping_Method {
/**
* Constructor for your shipping class
*
* @access public
* @return void
*/
public function __construct() {
$this->id = 'express_mail_flat_rate'; // Id for your shipping method. Should be uunique.
$this->method_title = __( 'Express Mail Flat Rate' ); // Title shown in admin
$this->method_description = __( 'Custom from plugin' ); // Description shown in admin
$this->enabled = "yes"; // This can be added as an setting but for this example its forced enabled
$this->title = "Express Mail Flat Rate"; // 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' => '35.00',
'calc_tax' => 'per_order'
);
// Register the rate
$this->add_rate( $rate );
}
}
class WC_Fed_Ex_Express_Ground extends WC_Shipping_Method {
/**
* Constructor for your shipping class
*
* @access public
* @return void
*/
public function __construct() {
$this->id = 'wc_fed_ex_express_ground'; // Id for your shipping method. Should be uunique.
$this->method_title = __( 'FedEx Ground (Domestic Only)' ); // Title shown in admin
$this->method_description = __( 'Custom from plugin' ); // Description shown in admin
$this->enabled = "yes"; // This can be added as an setting but for this example its forced enabled
$this->title = "FedEx Ground (Domestic Only)"; // 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' => '20.50',
'calc_tax' => 'per_order'
);
// Register the rate
$this->add_rate( $rate );
}
}
class Fedex_Overnight_Domestic extends WC_Shipping_Method {
/**
* Constructor for your shipping class
*
* @access public
* @return void
*/
public function __construct() {
$this->id = 'fedex_overnight_domestic'; // Id for your shipping method. Should be uunique.
$this->method_title = __( 'Fedex Overnight Domestic' ); // Title shown in admin
$this->method_description = __( 'Custom from plugin' ); // Description shown in admin
$this->enabled = "yes"; // This can be added as an setting but for this example its forced enabled
$this->title = "Fedex Overnight Domestic"; // 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' => '50.00',
'calc_tax' => 'per_order'
);
// Register the rate
$this->add_rate( $rate );
}
}
class International_FedEx extends WC_Shipping_Method {
/**
* Constructor for your shipping class
*
* @access public
* @return void
*/
public function __construct() {
$this->id = 'international_fedex'; // Id for your shipping method. Should be uunique.
$this->method_title = __( 'International FedEx' ); // Title shown in admin
$this->method_description = __( 'Custom from plugin' ); // Description shown in admin
$this->enabled = "yes"; // This can be added as an setting but for this example its forced enabled
$this->title = "International FedEx"; // 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' => '55.00',
'calc_tax' => 'per_order'
);
// Register the rate
$this->add_rate( $rate );
}
}
class International_Shipping_USPS extends WC_Shipping_Method {
/**
* Constructor for your shipping class
*
* @access public
* @return void
*/
public function __construct() {
$this->id = 'international_shipping_usps'; // Id for your shipping method. Should be uunique.
$this->method_title = __( 'International Shipping USPS' ); // Title shown in admin
$this->method_description = __( 'Custom from plugin' ); // Description shown in admin
$this->enabled = "yes"; // This can be added as an setting but for this example its forced enabled
$this->title = "International Shipping USPS"; // 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' => '45.00',
'calc_tax' => 'per_order'
);
// Register the rate
$this->add_rate( $rate );
}
}
}
function add_your_shipping_method( $methods ) {
$methods[] = 'WC_Express_Shipping';
$methods[] = 'Fedex_Overnight_Domestic';
$methods[] = 'WC_Fed_Ex_Express_Ground';
$methods[] = 'International_FedEx';
$methods[] = 'International_Shipping_USPS';
return $methods;
}
add_action( 'woocommerce_shipping_init', 'your_shipping_method_init' );
add_filter( 'woocommerce_shipping_methods', 'add_your_shipping_method' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment