Instantly share code, notes, and snippets.

Embed
What would you like to do?
AWD Weight/CountryShipping plugin modified to make it state based instead of country based. It has some minor tweaks as well
<?php
/**
* Plugin Name: AWD Weight/State Shipping
* Plugin URI: http://www.andyswebdesign.ie/blog/free-woocommerce-weight-and-country-based-shipping-extension-plugin/
* Description: Weight and State based shipping method for Woocommerce.
* Version: 1.0.1
* Author: Andy_P (modified by Mantish to make it state based)
/* Copyright 2012 andyswebdesign.ie
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
add_action('plugins_loaded', 'init_awd_shipping', 0);
function init_awd_shipping() {
if ( ! class_exists( 'WC_Shipping_Method' ) ) return;
class awd_Shipping extends WC_Shipping_Method {
function __construct() {
$this->id = 'awd_shipping';
$this->method_title = __( 'AWD Weight/State', 'woocommerce' );
$this->admin_page_heading = __( 'Weight based shipping', 'woocommerce' );
$this->admin_page_description = __( 'Define shipping by weight and state', 'woocommerce' );
//add_action( 'woocommerce_update_options_shipping_' . $this->id, array( &$this, 'sync_countries' ) );
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( &$this, 'process_admin_options' ) );
$this->init();
$this->display_state_groups();
}
/**
* init function
*/
function init() {
$this->init_form_fields();
$this->init_settings();
$this->enabled = $this->settings['enabled'];
$this->title = $this->settings['title'];
$this->state_group_no = $this->settings['state_group_no'];
//$this->sync_countries = $this->settings['sync_countries'];
$this->availability = 'specific';
$this->states = $this->settings['states'];
$this->type = 'order';
$this->tax_status = $this->settings['tax_status'];
$this->fee = $this->settings['fee'];
$this->options = isset( $this->settings['options'] ) ? $this->settings['options'] : '';
$this->options = (array) explode( "\n", $this->options );
}
function init_form_fields() {
global $woocommerce;
$this->form_fields = array(
'enabled' => array(
'title' => __( 'Enable/Disable', 'woocommerce' ),
'type' => 'checkbox',
'label' => __( 'Enable this shipping method', 'woocommerce' ),
'default' => 'no',
),
'title' => array(
'title' => __( 'Method Title', 'woocommerce' ),
'type' => 'text',
'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
'default' => __( 'Weight Based Shipping', 'woocommerce' ),
),
'tax_status' => array(
'title' => __( 'Tax Status', 'woocommerce' ),
'type' => 'select',
'description' => '',
'default' => 'taxable',
'options' => array(
'taxable' => __( 'Taxable', 'woocommerce' ),
'none' => __( 'None', 'woocommerce' ),
),
),
'fee' => array(
'title' => __( 'Handling Fee', 'woocommerce' ),
'type' => 'text',
'description' => __( 'Fee excluding tax. Enter an amount, e.g. 2.50. Leave blank to disable.', 'woocommerce' ),
'default' => '',
),
'options' => array(
'title' => __( 'Shipping Rates', 'woocommerce' ),
'type' => 'textarea',
'description' => __( 'Set your weight based rates for state groups (one per line). Example: <code>Max weight|Cost|state group number</code>. Example: <code>100|6.95|3</code>.', 'woocommerce' ),
'default' => '',
),
'state_group_no' => array(
'title' => __( 'Number of state groups', 'woocommerce' ),
'type' => 'text',
'description' => __( 'Number of groups of states sharing delivery rates (hit "Save changes" button after you have changed this setting).' ),
'default' => '3',
),
);
}
/*
* Displays state group selects in shipping method's options
*/
function display_state_groups() {
global $woocommerce;
// echo prp($this->settings['countries1']);
$number = $this->state_group_no;
$base_country = $woocommerce->countries->get_base_country();
for($counter = 1; $number >= $counter; $counter++) {
$this->form_fields['states'.$counter] = array(
'title' => sprintf(__( 'State Group %s', 'woocommerce' ), $counter),
'type' => 'multiselect',
'class' => 'chosen_select',
'css' => 'width: 450px;',
'default' => '',
'options' => $woocommerce->countries->get_states( $base_country )
);
}
}
/*
* This method is called when shipping is calculated (or re-calculated)
*/
function calculate_shipping($package = array()) {
global $woocommerce;
$rates = $this->get_rates_by_stategroup($this->get_stategroup($package));
$weight = $woocommerce->cart->cart_contents_weight;
$final_rate = $this->pick_smallest_rate($rates, $weight);
if($final_rate === false) return false;
$taxable = ($this->tax_status == 'taxable') ? true : false;
if($this->fee > 0 && $package['destination']['state']) $final_rate = $final_rate + $this->fee;
$label = ($final_rate == 0) ? 'Free!' : $this->title;
$rate = array(
'id' => $this->id,
'label' => $label,
'cost' => $final_rate,
'taxes' => '',
'calc_tax' => 'per_order'
);
$this->add_rate( $rate );
}
/*
* Retrieves the number of state group for state selected by user on checkout
*/
function get_stategroup($package = array()) {
$counter = 1;
while(is_array($this->settings['states'.$counter])) {
if (in_array($package['destination']['state'], $this->settings['states'.$counter]))
$state_group = $counter;
$counter++;
}
return $state_group;
}
/*
* Retrieves all rates available for selected state group
*/
function get_rates_by_stategroup($state_group = null) {
$rates = array();
if ( sizeof( $this->options ) > 0) foreach ( $this->options as $option => $value ) {
$rate = preg_split( '~\s*\|\s*~', trim( $value ) );
if ( sizeof( $rate ) !== 3 ) {
continue;
} else {
$rates[] = $rate;
}
}
foreach($rates as $key) {
if($key[2] == $state_group) {
$stategroup_rate[] = $key;
}
}
return $stategroup_rate;
}
/*
* Picks the right rate from available rates based on cart weight
*/
function pick_smallest_rate($rates,$weight) {
//if($weight == 0) return 0; // no shipping for cart without weight
if( sizeof($rates) > 0) foreach($rates as $key => $value) {
if($weight <= $value[0]) {
$postage[] = $value[1];
}
$postage_all_rates[] = $value[1];
}
if(sizeof($postage) > 0) {
return min($postage);
} else {
if (sizeof($postage_all_rates) > 0) return max($postage_all_rates);
}
return false;
}
function etz($etz) {
if(empty($etz) || !is_numeric($etz)) {
return 0.00;
}
}
public function admin_options() {
?>
<h3><?php _e('Weight and State based shipping', 'woocommerce'); ?></h3>
<p><?php _e('Lets you calculate shipping based on State and weight of the cart. Lets you set unlimited weight bands on per state basis and group states
that share same delivery cost/bands. For help and how to use go <a href="http://www.andyswebdesign.ie/blog/free-woocommerce-weight-and-country-based-shipping-extension-plugin/" target="_blank">here</a>', 'woocommerce'); ?></p>
<table class="form-table">
<?php
// Generate the HTML For the settings form.
$this->generate_settings_html();
?>
</table><!--/.form-table-->
<?php
}
} // end awd_Shipping
}
/**
* Add shipping method to WooCommerce
**/
function add_awd_shipping( $methods ) {
$methods[] = 'awd_shipping'; return $methods;
}
add_filter( 'woocommerce_shipping_methods', 'add_awd_shipping' );
?>
@SemBur

This comment has been minimized.

SemBur commented Jan 9, 2014

Hi, how can i modify the original plugin to show "no shipping" when there is no shipping rate for the product weigh. I see that you have it in your modification, but can't figure out what exactly do i need to modify... I just need this feature...

@suifengtec

This comment has been minimized.

suifengtec commented Apr 29, 2014

there may be some issues:
//add_action( 'woocommerce_update_options_shipping_' . $this->id, array( &$this, 'sync_countries' ) );
this sentence was commented,if uncomment this sentence and code the function named 'sync_countries',customers could not select this shipping method.

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