Skip to content

Instantly share code, notes, and snippets.

@WooForce
Created June 8, 2016 05:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WooForce/ce4171de8d87d2d845ebcb6ba1c0bba4 to your computer and use it in GitHub Desktop.
Save WooForce/ce4171de8d87d2d845ebcb6ba1c0bba4 to your computer and use it in GitHub Desktop.
Plugin to calculate My Custom Shipping Cost
<?php
/*
Plugin Name: My Custom Shipping
Description: Plugin to calculate My Custom Shipping Cost
Version: 1.0.0
Author: WooForce
Author URI: http://www.wooforce.com
*/
function my_custom_shipping_init() {
class my_custom_shipping_method extends WC_Shipping_Method {
public function __construct() {
$this->id = 'my_custom_shipping';
$this->method_title = __('My Custom Shipping', 'my-custom-shipping');
$this->enabled = 'yes';
$this->title = __('My Custom Shipping', 'my-custom-shipping');
$this->method_description = __('Plugin to calculate My Custom Shipping Cost', 'my-custom-shipping');
}
function calculate_shipping( $package = array() ) {
$shipping_cost = 0;
$destination_country = $package['destination']['country'];
switch($destination_country){
case 'US': $shipping_cost = 5; break;
case 'CA': $shipping_cost = 7; break;
}
$this->add_rate(array('id' => 'my_custom_shipping',
'label' => 'Shipping Cost',
'cost' => $shipping_cost,
'taxes' => '',
'calc_tax' => 'per_order'));
}
}
}
add_action( 'woocommerce_shipping_init', 'my_custom_shipping_init' );
function my_custom_shipping_method( $methods ) {
$methods[] = 'my_custom_shipping_method';
return $methods;
}
add_filter( 'woocommerce_shipping_methods', 'my_custom_shipping_method' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment