• I need to create a custom shipping method but the documentation on that point is a bit hazy to me. What I have sofar is this:

    class WC_PostNL extends WC_Shipping_Method {
            function __construct()
                $this->id           = 'postnl';
                $this->method_title = "PostNL";
                $this->title 	= "PostNL";
                $this->enabled = true;
            }
    
            function calculate_shipping() {
                global $woocommerce;
                $weight = $woocommerce->cart->cart_contents_weight;
    
                if (!$weight || $weight < 0.25) {
                    $postageNL = 3;
                    $postageBE = 9.35;
                } elseif ($weight < 2) {
                    $postageNL = 6.75;
                    $postageBE = 13;
                }
                elseif ($weight < 5) {
                    $postageNL = 6.75;
                    $postageBE = 19.50;
                }
                elseif ($weight < 10) {
                    $postageNL = 6.75;
                    $postageBE = 25;
                }
                elseif ($weight < 20) {
                    $postageNL = 12.20;
                    $postageBE = 34;
                }
                else {
                    $postageNL = 12.20;
                    $postageBE = 40.46;
                }
    
                $country = $woocommerce->customer->get_shipping_country();
    
                if ($country == "Nederland") {
                    $this->shipping_total = $postageNL;
                } else {
                    $this->shipping_total = $postageBE;
                }
            }
    
        }

    But during checkout no shipping costs are shown. What am I missing?

    http://wordpress.org/extend/plugins/woocommerce/

Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter Adam

    (@umbercode)

    Ok, so I finally got it to the point where it actually does something. but Why it does what it does, is beyond me.

    I disabled all other Shipping methods except my own. As a test I simply do this:

    $rate = array(
                    'id' 	=> $this->id,
                    'label' => "",
                    'cost' 	=> 5,
                );
    
                $this->add_rate( $rate );

    What happens is:

    1) At first this amount of 5 is added to the totals
    2) When I choose the country “Netherlands” It suddenly changes to 6,05
    3) When i choose “Belgium” It disapaers and states I must enter address. When I add something to the “State” field, it changes back to 6,05 again.

    Now I suppose the 6,05 is the 5 plus taxes? But if, so why does it not get added before I enter a country (besides the 5 I entered is already tax included)
    And why is it dependend on the state? I actually want to hide the state-field because it is redundant for my situation.

    The Documentation is not very clear on the Shipping API

    Plugin Author Mike Jolley (a11n)

    (@mikejolley)

    Well for a start, your country rule is wrong – get_shipping_country gives you a country code (NL).

    I suggest you look through some of the core shipping methods – at least that will show you how to return a cost.

    Of course, you could just shell out for table rates if this is a simple weight based shipping method by country? Table rates does it.

    Thread Starter Adam

    (@umbercode)

    Thanks for the reply.

    The client is low on funds (non-profit) and I do the project for next to nothing. 80$ is a bit to steep I am afraid.

    While I suspected the country code to be “Wrong” it was something I decided to c heck later, after I got the basics to work.
    Right now I do not look at the country code. I have a very basic class that just does $this->add_rate as described in my second post.
    I included this method for all countries.

    What I have right now is:

    class WC_PostNL extends WC_Shipping_Method {
            //var $id = 'postnl';
    
            function __construct() {
                $this->id           = 'postnl';
                $this->method_title = "PostNL";
                //$this->title 	= "PostNL";
                //$this->enabled = true;
                $this->init();
            }
    
            function init() {
                $this->init_form_fields();
                $this->init_settings();
    
                $this->enabled      = $this->settings['enabled'];
                $this->title        = $this->settings['title'];
                $this->availability = $this->settings['availability'];
                $this->countries    = $this->settings['countries'];
    
                add_action('woocommerce_update_options_shipping_' . $this->id, array(&$this, 'process_admin_options'));
            }
    
            function init_form_fields() {
                global $woocommerce;
    
                $this->form_fields = array(
                    'enabled'      => array(
                        'title'            => __('Enable/Disable', 'woocommerce'),
                        'type'             => 'checkbox',
                        'label'            => __('PostNL aan/uit zetten', 'woocommerce'),
                        'default'          => 'yes'
                    ),
                    'title'        => array(
                        'title'            => __('Method Title', 'woocommerce'),
                        'type'             => 'text',
                        'description'      => __('Titel', 'woocommerce'),
                        'default'          => __('PostNL', 'woocommerce')
                    ),
                    'availability' => array(
                        'title'            => __('Method availability', 'woocommerce'),
                        'type'             => 'select',
                        'default'          => 'all',
                        'class'            => 'availability',
                        'options'          => array(
                            'all'          => __('All allowed countries', 'woocommerce'),
                            'specific'     => __('Specific Countries', 'woocommerce')
                        )
                    ),
                    'countries'    => array(
                        'title'            => __('Specific Countries', 'woocommerce'),
                        'type'             => 'multiselect',
                        'class'            => 'chosen_select',
                        'css'              => 'width: 450px;',
                        'default'          => '',
                        'options'          => $woocommerce->countries->countries
                    )
                );
            }
    
            function calculate_shipping($package = array()) {
                $this->rates = array();
    
                $rate = array(
                    'id' 	=> 'dd',
                    'label' => "",
                    'cost' 	=> 5,
                );
    
                $this->add_rate( $rate );
    
            }
    
            function is_available($package) {
                global $woocommerce;
    
                if ($this->enabled == "no") {
                    return false;
                }
                return apply_filters('woocommerce_shipping_' . $this->id . '_is_available', true);
            }
    
        }

    Which yields the strange behaviour as described in my second post.

    Thread Starter Adam

    (@umbercode)

    the id=’dd’ is something left over from a test I did. It should be $this->id obviously. But it makes no difference for the result.

    Plugin Author Mike Jolley (a11n)

    (@mikejolley)

    I can’t help develop a custom gateway – you will need to look at examples of the core gateways I’m afraid.

    $75 isn’t steep if it saves you hours of time 😉 Took a long while to develop.

    Anyhow, 1 thing you may not be aware of – rates are cached which can cause odd behaviour. Clear transients cache from WooCommerce > System Status

    Thread Starter Adam

    (@umbercode)

    I am not asking you to help me write a custom gateway. I am asking -in general- if people can explain what I am doing wrong in my code that explains the behaviour.
    Time is not an issue for me, I’d rather happily spend weeks on a project and have something that works and I know why, than that I have to buy black boxes that never work 100% the way i need them to.
    Thanks for the tip about the cache. It does explain part of the problem, but not all. I guess I will just have to step through the code line by line to see how it works.
    I am 43 and I have been programming since I was 11 so I hope that qualifies me as at least a bit of an expert when I say I think Woocommerce is not very clear coded and documented. Just an observation, not meant to be any critique at you or anyone personal.

    Plugin Author Mike Jolley (a11n)

    (@mikejolley)

    Take a look at the add_rate method:
    http://wcdocs.woothemes.com/apidocs/source-class-WC_Shipping_Method.html#65

    Set a unique ID, and set the label.

    Thread Starter Adam

    (@umbercode)

    Thank for your help. I narrowed the problem down to 2 issues:

    1) Taxes on shipping are added to the ‘cost’ even if you set prices to be tax-included. So the ‘cost’ of Shipping must always be excluding taxes. However, when you do this and you select no country in the shipping field on the payment page, costs are shown without taxes, which is confusing (not just to me, but also for a customer). This problem I can work around.

    2) The cache you spoke of creates an interesting problem when you use weight-based costs. When you go to payment page and after that you go back to the shop and either add or remove items so the weight changes, the shipping cost does not change unless you flush the cache; which I cannot find how to do programatically? Could you tell me how to do that. If I can solve that problem I am golden 😉

    Plugin Author Mike Jolley (a11n)

    (@mikejolley)

    2 is a non-issue – if the cart or customer location changes, the cache will not apply.

    Thread Starter Adam

    (@umbercode)

    Really? hmm, then I have another problem, lol. Never mind, I’ll manage from here. thanks for your time.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘[Plugin: WooCommerce – excelling eCommerce] Need help with custom Shipping method class’ is closed to new replies.