Woocommerce Fedex Rest API shipping rates integration

By | April 8, 2025
Spread the love

To integrate FedEx shipping rates via the REST API into a WooCommerce store, here’s a step-by-step guide you can follow. This allows your WooCommerce checkout to fetch real-time shipping rates from FedEx using their REST API.

🔧 Requirements

  1. FedEx Developer Account

  2. WooCommerce Store

    • Running on WordPress with WooCommerce installed and configured.

    • SSL enabled for API communication.

  3. Optional but recommended: Child Theme or Custom Plugin for modifications.


🚀 Integration Steps

1. Get FedEx API Credentials

  • Go to FedEx Developer Portal

  • Create a project under “Manage Projects”

  • Enable Rates API (and optionally, Shipments, Labels, etc.)

  • Save your:

    • API Key

    • Secret

    • Account Number

    • Meter Number


2. Install a FedEx WooCommerce Plugin (Quick Option)

If you want a quick out-of-the-box solution:

You just enter your API credentials, set up services, and you’re good to go.


3. Manual Integration (Custom REST API)

If you prefer to write custom code:

A. Hook into WooCommerce Shipping Method

Register a custom shipping method in functions.php or a custom plugin:

add_action('woocommerce_shipping_init', 'custom_fedex_shipping_method_init');
function custom_fedex_shipping_method_init() {
    require_once 'class-fedex-shipping.php';
}

add_filter('woocommerce_shipping_methods', 'add_custom_fedex_shipping_method');
function add_custom_fedex_shipping_method($methods) {
    $methods['fedex_custom'] = 'Fedex_Custom_Shipping_Method';
    return $methods;
}

B. Create FedEx Shipping Class

File: class-fedex-shipping.php

class Fedex_Custom_Shipping_Method extends WC_Shipping_Method {
    public function __construct() {
        $this->id                 = 'fedex_custom';
        $this->method_title       = __('FedEx Custom');
        $this->method_description = __('FedEx Shipping via REST API');
        $this->enabled            = "yes";
        $this->title              = "FedEx";

        $this->init();
    }

    function init() {
        $this->init_form_fields();
        $this->init_settings();
    }

    public function calculate_shipping($package = array()) {
        $rate = $this->get_fedex_rate($package);
        if ($rate) {
            $this->add_rate(array(
                'label' => 'FedEx Standard',
                'cost'  => $rate,
            ));
        }
    }

    private function get_fedex_rate($package) {
        $weight = $package['contents_weight'];
        $dest = $package['destination'];

        $body = [
            "accountNumber" => ["value" => "YOUR_ACCOUNT_NUMBER"],
            "requestedShipment" => [
                "dropOffType" => "REGULAR_PICKUP",
                "serviceType" => "FEDEX_GROUND",
                "packagingType" => "YOUR_PACKAGING",
                "shipper" => [
                    "address" => [
                        "postalCode" => "YOUR_ZIP",
                        "countryCode" => "US"
                    ]
                ],
                "recipient" => [
                    "address" => [
                        "postalCode" => $dest['postcode'],
                        "countryCode" => $dest['country']
                    ]
                ],
                "packages" => [[
                    "weight" => [
                        "units" => "LB",
                        "value" => $weight
                    ]
                ]]
            ]
        ];

        $response = wp_remote_post('https://apis.fedex.com/rate/v1/rates/quotes', [
            'headers' => [
                'Content-Type'  => 'application/json',
                'Authorization' => 'Bearer ' . $this->get_access_token(),
            ],
            'body' => json_encode($body)
        ]);

        if (!is_wp_error($response)) {
            $data = json_decode($response['body'], true);
            return $data['output']['rateReplyDetails'][0]['ratedShipmentDetails'][0]['totalNetCharge']['amount'];
        }

        return false;
    }

    private function get_access_token() {
        $response = wp_remote_post('https://apis.fedex.com/oauth/token', [
            'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
            'body'    => [
                'grant_type'    => 'client_credentials',
                'client_id'     => 'YOUR_CLIENT_ID',
                'client_secret' => 'YOUR_CLIENT_SECRET',
            ]
        ]);

        if (!is_wp_error($response)) {
            $body = json_decode($response['body'], true);
            return $body['access_token'];
        }

        return '';
    }
}

Replace placeholders like YOUR_ACCOUNT_NUMBER, YOUR_CLIENT_ID, etc., with your actual credentials.

🧪 Testing

  1. Set WooCommerce to Debug Mode (in shipping settings).

  2. Use a real U.S. ZIP and destination in test checkout.

  3. Validate FedEx rates are appearing correctly.