0
votes

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

2

2 Answers

2
votes

First of all, I think you should instanciate the class in the WC_Overnight_Express_init function because as is, there is no way the contruct method could be run.

Then, there is an option in woocommerce system tools that enables the debug for shipping methods, it really is a good idea to activate it during your development (sorry the screenshot is in french, but you get the idea, right?) :

screenshot

0
votes

Edit

Try place add_action and add_filter code outside if clause. I know that sound crazy but often it helps.

After some thinking I'am pretty sure that problem lays in storing data in $_SESSION. Please follow my advice and do all calculations in calculate_shipping method. Of course you can place there any static value for test perpouse. Or make var_dump($_SESSION) in calculate_shipping to make sure everything is as expected (on ajax I often use file_put_contents or flip/whoops).

Shipping calculation

Next in calculate_shipping metod you can get cart contents and make your magic:

function calculate_shipping() {
    $cart = WC_Cart::get_cart() 
    foreach($cart as $item){
        //magic
    }
}

Old Answer

Correct hooks

You should hook your shipping logic to:

add_action( 'woocommerce_shipping_init', 'tutsplus_shipping_method' );

function add_tutsplus_shipping_method( $methods ) {
    $methods[] = 'WC_Overnight_Express';
    return $methods;
}

add_filter( 'woocommerce_shipping_methods', 'add_tutsplus_shipping_method' );

Shipping calculation

Next in calculate_shipping metod you can get cart contents and make your magic:

function calculate_shipping(){
    $cart = WC_Cart::get_cart() 
    foreach($cart as $item){
        //magic
    }
}

This may help, for live updating cart review:

https://stackoverflow.com/a/47133456/8888443

More help

Look at this tuturial: https://code.tutsplus.com/tutorials/create-a-custom-shipping-method-for-woocommerce--cms-26098

If you are new to wordpress developing I highly advice you to get familiar with actions/hooks. Both woocommerce and wordpress has good reference.

https://codex.wordpress.org/Plugin_API/Action_Reference

https://docs.woocommerce.com/wc-apidocs/hook-docs.html