I hope you can help.
I’ve written a custom shipping plugin which works great, but it’s postcode dependant.
When you enter your address the shipping information needs updating, but this is not happening. I’ve seen it refresh before, but not sure how.

In Shipping options I have “Enable the shipping calculator on the cart page” ticked and tried clearing the transients, but nothing changes.

Any help would be much appreciated.

Thanks
Mark


Shipping Class Code if it helps:

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* Check if WooCommerce is active
*/
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
function custom_delivery_init() {
if ( ! class_exists( 'WC_Custom_Delivery' ) ) {
class WC_Custom_Delivery extends WC_Shipping_Method {
/**
* Constructor for your shipping class
*
* @access public
* @return void
*/
public function __construct() {
$this->id = 'custom_delivery'; // Id for your shipping method. Should be uunique.
$this->method_title = __( 'Takeaway Delivery' ); // Title shown in admin
$this->method_description = __( 'Delivery for use on Takeaways ' ); // Description shown in admin
$this->enabled = "yes"; // This can be added as an setting but for this example its forced enabled
$this->title = "Delivery"; // This can be added as an setting but for this example its forced.
$this->init();
}
/**
* Init your settings
*
* @access public
* @return void
*/
function init() {
// Load the settings API
$this->init_form_fields(); // This is part of the settings API. Override the method to add your own settings
$this->init_settings(); // This is part of the settings API. Loads settings you previously init.
// Save settings in admin if you have any defined
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
}
/**
* calculate_shipping function.
*
* @access public
* @param mixed $package
* @return void
*/
public function calculate_shipping( $package ) {
global $wpdb, $websiteconfig;
$postcode = "CV34 5SB";
$query = "SELECT * FROM postcodes WHERE postcode = '$postcode' LIMIT 1";
$res = $wpdb->get_results($query);
list($shoplat,$shoplon) = explode(",",$websiteconfig['latlon']);
$lat = $res[0]->latitude;
$lon = $res[0]->longitude;
$distance = round(distance($shoplat,$shoplon,$lat,$lon),2);
$delivery = json_decode($websiteconfig['delivery']);
foreach ($delivery as $miles => $price){
    if (!isset($cost) && ($miles > $distance)){
        $cost = $price;             
    }
    $furthest = $miles;
}
if (!isset($cost)){
    $rate = array(
        'id' => $this->id,
        'label' => '',
        'cost' => false,
        'calc_tax' => 'per_item'
    );
} else {
    if ($package->contents_cost >= 25){
        $rate = array(
            'id' => $this->id,
            'label' => 'Delivery on orders over £25',
            'cost' => '0',
            'calc_tax' => 'per_item'
        );
    } else {
        $rate = array(
            'id' => $this->id,
            'label' => 'Delivered',
            'cost' => $cost,
            'calc_tax' => 'per_item'
        );
    }
}
// Register the rate
$this->add_rate( $rate );
}
}
}
}
add_action( 'woocommerce_shipping_init', 'custom_delivery_init' );
function custom_delivery( $methods ) {
$methods[] = 'WC_Custom_Delivery';
return $methods;
}
add_filter( 'woocommerce_shipping_methods', 'custom_delivery' );
}

Read more here: WooCoomerce: Custom Shipping needs updating on address change


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Related Wordpress search:

, , , , ,

Wordpress related questions and answers: