WordPress.org

Ready to get started?Download WordPress

Forums

AWD Weight/Country Shipping for WooCommerce
Separate shipping for states/provinces (35 posts)

  1. touteptite
    Member
    Posted 1 year ago #

    Hi,

    I know that someone posted something similar for Italy's Island...

    But it would be great to be able to choose given states/provinces for a specific Country.

    I live in Canada (province of Quebec) and we have a company that offers very good rates but ONLY for provinces of Ontario and Quebec... So it would be great to have the option to choose specific provinces!

    Otherwise, what a great plugin! :)

    http://wordpress.org/extend/plugins/awd-weightcountry-shipping/

  2. chetan0412
    Member
    Posted 1 year ago #

    Ya right,

    If we have option to add state to country than it will helpful ...

  3. chetan0412
    Member
    Posted 1 year ago #

    Admin ,
    Can you add shipping cost for STATES?

    My business is limited to One country only...I want to setup Shipping cost for states only....

    Thanks...

  4. chetan0412
    Member
    Posted 1 year ago #

    Hi,

    After 6 Days no one ready to reply??????

  5. mantish
    Member
    Posted 1 year ago #

    Hi,

    I modified the plugin to do what you need. It took me a couple of hours.

    I only need to sell in one country so it wasn't too difficult. Making it work for several countries would be more complex.

    I'm not sure where to post all the code...is there a way to send messages to other users?

  6. chetan0412
    Member
    Posted 1 year ago #

    Mantish,

    Thanks for reply...

    If possible to sell in different state than good(Limited to one country only),

    which information u need?

    My country is INDIA.

    thanks.

  7. mantish
    Member
    Posted 1 year ago #

    chetan0412, here's what I did:

    First, replace all instances of country(ies) to state(states)

    Second, delete all functions or lines where sync_states is mentioned

    Third, change display_state_groups() function into this:

    function display_state_groups() {
    
    	global $woocommerce;
    	$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 )
    		);
    	}
    }

    hope it works for you

  8. chetan0412
    Member
    Posted 1 year ago #

    Dear mantish,

    Thanks for prompt response..thanks again,

    Dear m beginner.. mstill not understanding what to do..

    1. Replace all instances of country(ies) to state(states)
    Q- where ? in shipping-awd.php? or class-wc-countries.php?

    2. delete all functions or lines where sync_states is mentioned
    Q- where ? in shipping-awd.php? or class-wc-countries.php?

    and third also..

    Q- where ? in shipping-awd.php? or class-wc-countries.php?

    if this change is applicable to shipping-awd.php in only than send me to my mail [ redacted, support is offered via the forum and not email ]

    again thank you so much...

  9. mantish
    Member
    Posted 1 year ago #

    Hi,

    all changes should be made in the plugin file: shipping-awd.php

  10. chetan0412
    Member
    Posted 1 year ago #

    thanks i ll try to complete..

  11. Luci_Fernandez
    Member
    Posted 1 year ago #

    This is exactly what I need, mantish, thank you very much!

  12. florbv
    Member
    Posted 1 year ago #

    hello mantish!

    I've tried doing your fix mentioned above but unfortunately I got a fatal error stating that Call to a member function get_states() on a non-object in..../shipping-awd.php on line 138

    Hope you can help me on this. Thank you.

  13. florbv
    Member
    Posted 1 year ago #

    Than happens when I click the woocommerce > settings link

  14. riyota
    Member
    Posted 1 year ago #

    mantish thanks for the snippet, just what I needed, and to take out the sync parts.
    I'm also trying to adjust the extension to make it into flat rate state based shipping extension. So I would like groups of states to have different flat rates for shipping. I tried to change in the function pick_smallest_rate but my change just made all shipping to become 1 in my case 1 yen. Would appreciate if someone could direct me in a correct direction.

    function pick_smallest_rate($rates,$weight) {
    
      if($weight == 0) return 1; // changed this return to 1 from 0 
    
            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;
        }
  15. mantish
    Member
    Posted 1 year ago #

    florbv, if you post your code somewhere I'd take a look.
    riyota, I think your problem is that orders have 0 weight. Maybe commenting that first line is all that's needed to make it work.

  16. florbv
    Member
    Posted 1 year ago #

    thnx for the response mantish.

    I've tried to do it again(the 3 steps you've mentioned) but now I am getting a parsing error something like this: T_PUBLIC...I guess this is all about deleting the sync_states function and lines. maybe I just left an open bracket or parenthesis or something...but I couldn't figure it out. How may you help me this this pls? Where can I post my code for you to check?

  17. florbv
    Member
    Posted 1 year ago #

    Yes! Finally I figured it out. There's indeed a close curly bracket in a function of sync_states that has not been deleted. However, I still have a problem mantish. Upon entering the number of groups of states, nothing appears on the fields. By the way, I've added a PH.php in the /i18n/states folder in woocommerce as I noticed that there is no available for my country(Philippines). I just copied one the files there and renamed accordingly. Hope this helps. Hope to hear from you again soon. Thank you so much.

  18. florbv
    Member
    Posted 1 year ago #

    By the way, I already set the base location in the General Options to my country(Philippines) and also the allowed countries to 'specific' and enter 'Philippines' in the specific countries field. I notice the result of my adding the PH.php file because now, upon selecting my Base Location, the states I've entered in the PH.php file already appear under my country(Philippines). However, when I go to Shipping tab and AWD Weigh/state, still the states don't appear. When I try to type something, it always gives me a "No results match..." message. I've also tried to change the specific allowed country to the US but still the same result.

  19. mantish
    Member
    Posted 1 year ago #

    florbv, you can put your code in github.
    By the way, adding the states (or any othe customization) inside the plugin folder is not recommended, as it may get lost whan you upgrade the plugin.
    You can add the states in your theme functions.php like this:

    add_filter( 'woocommerce_states', 'custom_woocommerce_states' );
    function custom_woocommerce_states( $states )
    {
    	$states['PH'] = array(
    		'XXX' => 'Xxxxx',
    		...
    	);
    	return $states;
    }
  20. mantish
    Member
    Posted 1 year ago #

    I just published my code here: https://gist.github.com/Mantish/5658280
    You can compare to see if there's something odd

  21. florbv
    Member
    Posted 1 year ago #

    Thanks mantish! I've tried copying your codes and they worked! Great! Just have some more followup questions though. I have a theme child. where do I put those customizations so as not to be overwritten during upgrade. I've tried creating a functions.php file in my theme child directory and copied the code above but it just gave me an error. So I tried copying the whole functions.php from my parent theme to my child theme, added the coded above, and deleted the original functions.php in my parent theme directory and it worked. Did I do it right?

    Re shipping-awd.php file, I've tried creating a directory in my child theme and it looks like this: /twentyeleven_child/awd-weightcountry-shipping and tried to place your shipping-awd.php file there but it didn't work. The entire AWD Weight/State shipping option disappeared in the Shipping tab of WooCommerce settings so what I did was just put it in the plugin's original location folder(plugins/awd-weightcountry-shipping) and it worked. What should I suppose to do to protect it from future upgrades?

    Thank you so much for being kind. Really appreciate it. Am just a newbie in this field.:)

  22. florbv
    Member
    Posted 1 year ago #

    One more thing mantish, is it possible for me to add another similar shipping method like this? Now with another shipping courier with different computations(still weight and state-based) to give my customers more options which shipping courier they like based on their shipping address coz sometimes their location is not covered by a certain courier. Hope to hear from you again. Thank you.

  23. florbv
    Member
    Posted 1 year ago #

    Hello mantish! Glad I did it! Last question solved already. All I wanna know right now is how to keep safe these customized files from future updates/upgrades. Thank you so much!

  24. mantish
    Member
    Posted 1 year ago #

    florbv, I haven't work with child themes, so I wouldn't know what's the right way to go. You can check wordpress documentation.

    As for the customized files, I was talking about Woocommerce, which is common to have it updated once in a while. Regarding this shipping plugin, we're actually hacking it, so we have to overwrite the shipping-awd.php and NEVER update it.

  25. florbv
    Member
    Posted 1 year ago #

    Thanks mantish. will refer to wordpress documentation then. Are there options on setting plugin updates not to update automatically?

  26. spybubblegum
    Member
    Posted 1 year ago #

    cool! Can you share that on github florbv? do you have a gist of it? ...

  27. saxekyss
    Member
    Posted 11 months ago #

    I can't input state names. How did you do it?

  28. Chirag Vora
    Member
    Posted 9 months ago #

    Thanks for this post. This feature must be added to the plugin or a separate plugin must be developed for state/province based pricing for shipping. Many people are looking for this.

  29. Art Project Group
    Member
    Posted 9 months ago #

    I made some changes to the plugin and mantish code for spanish users. Now we can use a different shipping rate for Ceuta and Melilla, Baleares, Canarias and the rest of Spain, Portugal and Andorra.

    Now you can select state and/or country, and use more than one state or country at same time in the shipping rates (comma separated).

    Here you have the full new code. Please, enjoy it:

    <?php
    /**
     * Plugin Name: AWD Weight/State/Country Shipping
     * Plugin URI: http://www.andyswebdesign.ie/blog/free-woocommerce-weight-and-country-based-shipping-extension-plugin/
     * Description: Weight and State/Country based shipping method for Woocommerce.
     * Version: 1.0.1
     * Author: Andy_P (modified by Mantish to make it state based) Art Project Group final version
    /*  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();
    	        $this->display_country_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->country_group_no   		= $this->settings['country_group_no'];
                $this->sync_countries 			= $this->settings['sync_countries'];
                $this->availability             = 'specific';
                $this->states                   = $this->settings['states'];
    			$this->countries 	  			= $this->settings['countries'];
                $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/country group number separated by comma</code>. Example: <code>100|6.95|S1,C3</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',
                        ),
                    'country_group_no' => array(
                        'title'                 => __( 'Number of country groups', 'woocommerce' ),
                        'type'                 	=> 'text',
                        'description'			=> __( 'Number of groups of countries sharing delivery rates (hit "Save changes" button after you have changed this setting).' ),
                        'default' 				=> '3',
                        ),
                    'sync_countries' => array(
                        'title'                 => __( 'Add countries to allowed', 'woocommerce' ),
                        'type'                 	=> 'checkbox',
                        'label' 				=> __( 'Countries added to country groups will be automatically added to Allowed Countries
    												in <a href="/wp-admin/admin.php?page=woocommerce_settings&tab=general">General settings tab.</a>
    												This makes sure countries defined in country groups are visible on checkout.
    												Deleting country from country group will not delete country from Allowed Countries.', 'woocommerce' ),
                        'default' 				=> 'no',
                        ),
                    );
            }
    
            /*
            * 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['S'.$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 )
                        );
                }
            }
    
            /*
    	    * Displays country group selects in shipping method's options
    	    */
        	function display_country_groups() {
    
    	        global $woocommerce;
       		//   echo prp($this->settings['countries1']);
    	        $number = $this->country_group_no;
    
    			for($counter = 1; $number >= $counter; $counter++) {
    
        	        $this->form_fields['C'.$counter] =  array(
            	            'title'     => sprintf(__( 'Country Group %s', 'woocommerce' ), $counter),
    	                    'type'      => 'multiselect',
    	                    'class'     => 'chosen_select',
        	                'css'       => 'width: 450px;',
    	                    'default'   => '',
    	                    'options'   => $woocommerce->countries->countries
    	            );
    	        }
    	    }
    
            /*
            * This method is called when shipping is calculated (or re-calculated)
            */
            function calculate_shipping($package = array()) {
    
                global $woocommerce;
    
    			$group = $this->get_group($package);
    			$rates = $this->get_rates_by_group($group);
    			if ($group[0] == 'S') $destination = $package['destination']['state'];
    			else $destination = $package['destination']['country'];
    
                $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 && $destination) $final_rate = $final_rate + $this->fee;
    
                $rate = array(
                    'id'        => $this->id,
                    'label'     => $this->title,
                    'cost'      => $final_rate,
                    'taxes'     => '',
                    'calc_tax'  => 'per_order'
                    );
    
                $this->add_rate( $rate );
            }
    
            /*
            * Retrieves the number of state/country group for state selected by user on checkout
            */
            function get_group($package = array()) {
    
                $counter = 1;
    
                while(is_array($this->settings['S'.$counter])) {
                    if (in_array($package['destination']['state'], $this->settings['S'.$counter])) $group = 'S'.$counter;
    
                    $counter++;
                }
    
                if (isset($group)) return $group;
    
                $counter = 1;
    
                while(is_array($this->settings['C'.$counter])) {
                    if (in_array($package['destination']['country'], $this->settings['C'.$counter])) $group = 'C'.$counter;
    
                    $counter++;
                }
    
                return $group;
            }
    
        	/*
    	    * Retrieves all rates available for selected group
    	    */
            function get_rates_by_group($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) {
    				$groups = explode(",", $key[2]);
    				foreach($groups as $group_key)
    				{
    	                if ($group_key == $group) {
        	                $group_rate[] = $key;
            	        }
    				}
                }
                return $group_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;
                }
            }
    
    	    /*
    	    * Uptades Allowed Countries with countries added to country groups
    	    */
    	    function sync_countries() {
    
    	        if ($this->settings['sync_countries'] == 'yes') {
    	            $countries = $this->get_cg_countries();
    	            update_option('woocommerce_specific_allowed_countries', $countries);
    	        }
    	    }
    
    	    /*
    	     * Retrieves countries from country groups and merges them with Allowed Countries array
    	     */
        	function get_cg_countries() {    
    
                $counter = 1;
                $countries = array();
    
                while(is_array($this->settings['countries'.$counter])) {
                    $countries = array_merge($countries, $this->settings['countries'.$counter]);
    
                	$counter++;
                }
    
                $allowed_countries = get_option( 'woocommerce_specific_allowed_countries' );
    
                if (is_array($allowed_countries)) $countries = array_merge($countries, $allowed_countries);
                $countries = array_unique($countries);
            	return $countries;
        	}
    
            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' );
    
    ?>
  30. VD11
    Member
    Posted 9 months ago #

    Emilio José Calvo,
    That is brilliant! Works perfectly. Thank you!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.