I am fairly new to php and woocommerce. I am working on a ecommerce website which uses wordpress and woocommerce. The company uses and API to calculate the shipping costs based on area and dimensions of the items in the cart as well as item quantity. I have managed to parse all needed values to the API and i do get a response. I created a custom shipping plugin for the 3 testing rates provided. in my script i create sessions that holds the total of each shipping method inside it like this:
$_SESSION['overnight']= $quoteResponse->results[0]->rates[2]->total;
and then in my custom shipping plugin i start the session and set the cost equal to the value inside the session:
<?php
session_start();
* Check if WooCommerce is active
*/
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
function WC_Overnight_Express_init() {
if ( ! class_exists( 'WC_Overnight_Express' ) ) {
class WC_Overnight_Express extends WC_Shipping_Method {
/**
* Constructor for your shipping class
*
* @access public
* @return void
*/
public function __construct() {
$this->id = 'overnight express'; // Id for your shipping method. Should be uunique.
$this->method_title = __( 'Overnight Express' ); // Title shown in admin
$this->method_description = __( 'Overnight Express Shipping method' );
$this->enabled = "yes"; // This can be added as an setting but for this example its forced enabled
$this->title = "Overnight Express"; // 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() {
$rate = array(
'id' => $this->id,
'label' => $this->title,
'cost' =>$_SESSION['overnight'],
'calc_tax' => 'per_item'
);
// Register the rate
$this->add_rate( $rate );
}
}
}
}
add_action( 'woocommerce_shipping_init', 'WC_Overnight_Express_init' );
function add_overnight_method( $methods ) {
$methods[] = 'WC_Overnight_Express';
return $methods;
}
add_filter( 'woocommerce_shipping_methods', 'add_overnight_method' );
}
?>
My problem is my after the quotes return from the API they dont update shipping totals they display "free" and only udpate if I open my cart and go back to the checkout page. What I wanna do is update them at runtime when the totals are returned from the API. This is my first post on stackoverlow And any help will be greatly appreciated. Thanks