1
votes

I use custom plugin for Woocommerce shipping method. The plugin run a new class which extends WC_Shipping_MEthod, but the problem is the 'cost' price is calculated in other function outsite the class.

So in the current class I have:

        public function calculate_shipping( $package=array() ) {

         /*   $this->add_rate( array(
                    'id'    => $this->id . $this->instance_id,
                    'label' => $this->title,
                    'cost'  => 0,
            ) ); */

            $rate = array(
                'id' => $this->id,
                'label' => $this->title,
                'cost' => 0,
                'taxes' =>false,
                //'calc_tax' => 'per_item'
            );

             // Register the rate
            $this->add_rate( $rate );

        }

Outside the class, in another file, I have a function, which calculates the cost for shipping:

//add shipping cost to checkout total
add_action('woocommerce_cart_calculate_fees', 'woo_add_cart_fee');

function woo_add_cart_fee() {

    global $woocommerce;
    $woocommerce->shipping;
    $wc_econt = new WC_Econt_Shipping_Method;

     if ( is_checkout() && (bool)$wc_econt->inc_shipping_cost == TRUE ) {
        if(!isset($_SESSION)){ 
            session_start(); 
        } 
        //write_log('session econt customer shipping cost:'.$_SESSION['econt_shipping_cost']);
        $extracost = (isset($_SESSION['econt_shipping_cost']) ? $_SESSION['econt_shipping_cost'] : 0 );
        if ($extracost != '0') {
            WC()->cart->add_fee(__('Econt Express Shipping Method','woocommerce-econt'), $extracost);       
        }
    }
}

If just change 0 to $extracode in the class function it doesn't update the shipping cost on checkout page:

public function calculate_shipping( $package=array() ) {

     /*   $this->add_rate( array(
                'id'    => $this->id . $this->instance_id,
                'label' => $this->title,
                'cost'  => $extracost,
        ) ); */

        $rate = array(
            'id' => $this->id,
            'label' => $this->title,
            'cost' => $extracost,
            'taxes' =>false,
            //'calc_tax' => 'per_item'
        );

         // Register the rate
        $this->add_rate( $rate );

    }

I guess the class run before the woo_add_cart_fee function, and that's why it cannot get the value of $extracost variable.

How to resolve this?

1

1 Answers

0
votes

In the function to calculate the shipping cost, add a session variable with your new calculated value like this:

//add shipping cost to checkout total
add_action('woocommerce_cart_calculate_fees', 'woo_add_cart_fee');

function woo_add_cart_fee() {

    global $woocommerce;
    $woocommerce->shipping;
    $wc_econt = new WC_Econt_Shipping_Method;

     if ( is_checkout() && (bool)$wc_econt->inc_shipping_cost == TRUE ) {
        if(!isset($_SESSION)){ 
            session_start(); 
        } 
        //write_log('session econt customer shipping cost:'.$_SESSION['econt_shipping_cost']);
        $extracost = (isset($_SESSION['econt_shipping_cost']) ? $_SESSION['econt_shipping_cost'] : 0 );
        if ($extracost != '0') {
            WC()->session->set( 'Econt_Express_Shipping_Method' , $extracost );       
        }
    }
}

Then on the calculate shipping function use the WC session variable to set the value for the cost like below:

public function calculate_shipping( $package=array() ) {

 /*   $this->add_rate( array(
            'id'    => $this->id . $this->instance_id,
            'label' => $this->title,
            'cost'  => 0,
    ) ); */

    $rate = array(
        'id' => $this->id,
        'label' => $this->title,
        'cost' => WC()->session->get('Econt_Express_Shipping_Method' ),
        'taxes' =>false,
        //'calc_tax' => 'per_item'
    );

     // Register the rate
    $this->add_rate( $rate );

}

Use the javascript below to update your new shipping value at checkout:

$('body').trigger('update_checkout', { update_shipping_method: true });

You may want to change the value of billing address or shipping address or country to be able to update your value if you use the above code more than once on checkout. This is also where I stuck and am forced to fake these changes so that Woocommerce can update my shipping value...am still looking for the solution though...lol :)

$("#billing_address_1").trigger("keypress").val(function(i,val){return val + 'a';});