0
votes

I've created a very small and simple custom shipping method for WooCommerce, it basically calculates the shipping costs based on city and weight.

class WC_Chilexpress_Shipping_Method extends WC_Shipping_Method {

... 

public function calculate_shipping($package){

    $small_price = get_post_meta($shipping[0]->ID, 'chxp_small_price', true);
    $medium_price = get_post_meta($shipping[0]->ID, 'chxp_medium_price', true);

    if($weight < 6  && $weight >= 3) :
        $cost = $medium_price + 1000;
    elseif($weight < 10 && $weight >= 6) :
        $cost = $medium_price + 1700;
    elseif ($weight >= 10) :
        $cost = $medium_price + 2200;
    elseif($weight < 3) : 
        $cost = (int)$small_price + 700;
    endif;

    $this->add_rate( array(
      'id'  => $this->id,
      'label' => $this->title,
      'cost'    => $cost
    ));                        

    return $cost;
}

Everything works fine using a billing address only, but if is select a "Shipping address" the checkout says that "Pick up in store" was selected.

The full code is here:

https://gist.github.com/albertojm/55a9319dadc36c936c84a3904d114fbd

https://gist.github.com/albertojm/8e2e3fe2d90e19dc1875ef04ab565125

1
Do you use your code as a plugin? Have you adapted the code from this tutorial?LoicTheAztec
I've adapted a similar code to that (can't recall where I've found it).Alberto

1 Answers

0
votes

The issue was that, after calculating the shipping costs Woocommerce re-sends the post data to the checkout page but without s_state or state post vars.

Since I am calculating the shipping cost according to those two variables, I I had to save them into a session var and calculate from here.