简体   繁体   中英

Woocommerce set shipping method, get shipping cost

In my functions.php file, I need to be able to set the shipping method (there's only one for this site) and then return the cost for shipping (there are a few costs set).

I can see the flat_rate shipping using this:

foreach (WC()->shipping->get_shipping_methods() as $key => $value){
    echo $key;
}

So it's definitely there.

Basically, I want the shipping cost returned in from an API call. I have the API routed and all that working. It calls this function that I picked up somewhere, don't recall currently:

function pc_get_shipping(){
  $ret = array();
    foreach( WC()->session->get('shipping_for_package_0')['rates'] as $method_id => $rate ){
        if( WC()->session->get('chosen_shipping_methods')[0] == $method_id ){
            $rate_label = $rate->label; // The shipping method label name
            $rate_cost_excl_tax = floatval($rate->cost); // The cost excluding tax
            // The taxes cost
            $rate_taxes = 0;
            foreach ($rate->taxes as $rate_tax)
                $rate_taxes += floatval($rate_tax);
            // The cost including tax
            $rate_cost_incl_tax = $rate_cost_excl_tax + $rate_taxes;

            $ret[] = array('label' => $rate_label, 'total' => WC()->cart->get_cart_shipping_total());
        } 
    }
  return $ret;
}

But that gives me just an empty array, probably because WC()->session->get('shipping_for_package_0')['rates'] evaluates to an empty array.

TL:DR

  • Guest customer shipping info is saved via WC()->customer->set_shipping_address_1(wc_clean($value)); (etc for all values)

  • Guest customer shipping info is correctly returned using WC()->customer->get_shipping() , so I believe it is being set correctly.

  • The shipping method flat_rate is available via WC()->shipping->get_shipping_methods() .

  • How do I set the shipping method for the current order in functions.php, in a method that will be called via REST API.

  • How do I get the calculated shipping cost for the current order in functions.php, in a method that will be called via REST API.

First on your API Response, you should need to set the cost value as custom data in WC_Session , with something like (where $value is the response cost value from your API) :

if( ! WC()->session->__isset( 'shipping_cost' ) && ! empty($value) ){
    WC()->session->set( 'shipping_cost', $value );
}

you may need to ajax refresh checkout page using jQuery: $('body').trigger('update_checkout');

Then you will use this hooked function:

add_filter('woocommerce_package_rates', 'shipping_cost_based_on_api', 12, 2);
function shipping_cost_based_on_api( $rates, $package ){
    if( WC()->session->__isset( 'shipping_cost' ) ) {

        // Loop through the shipping taxes array
        foreach ( $rates as $rate_key => $rate ){
            $has_taxes = false;

            if( 'flat_rate' === $rate->method_id ){
                // Get the initial cost
                $initial_cost = $new_cost = $rates[$rate_key]->cost;

                // Get the new cost
                $new_cost = WC()->session->get( 'shipping_cost' );

                // Set the new cost
                $rates[$rate_key]->cost = $new_cost;

                // Taxes rate cost (if enabled)
                $taxes = [];
                // Loop through the shipping taxes array (as they can be many)
                foreach ($rates[$rate_key]->taxes as $key => $tax){
                    if( $rates[$rate_key]->taxes[$key] > 0 ){
                        // Get the initial tax cost
                        $initial_tax_cost = $new_tax_cost = $rates[$rate_key]->taxes[$key];
                        // Get the tax rate conversion
                        $tax_rate    = $initial_tax_cost / $initial_cost;
                        // Set the new tax cost
                        $taxes[$key] = $new_cost * $tax_rate;
                        $has_taxes   = true; // Enabling tax
                    }
                }
                if( $has_taxes )
                    $rates[$rate_key]->taxes = $taxes;
            }
        }
    }
    return $rates;
}

// Enabling, disabling and refreshing session shipping methods data
add_action( 'woocommerce_checkout_update_order_review', 'refresh_shipping_methods', 10, 1 );
function refresh_shipping_methods( $post_data ){
    $bool = true;
    if ( WC()->session->__isset('shipping_cost' ) ) $bool = false;

    // Mandatory to make it work with shipping methods
    foreach ( WC()->cart->get_shipping_packages() as $package_key => $package ){
        WC()->session->set( 'shipping_for_package_' . $package_key, $bool );
    }
    WC()->cart->calculate_shipping();
}

Code goes in function.php file of your active child theme (or active theme). It should work.

Based on: Remove shipping cost if custom checkbox is checked in WooCommerce Checkout

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM