skip to Main Content

Previous / Related Question: Display ALL available shipping methods for each specific order on admin edit order pages in Woocommerce

Currently in my WooCommerce based site, I am wanting to display the available shipping methods and prices on the order edit page.

It does not display the data as I want. For example, the output of my code so far results in:

Method 1
Method 2
Method 3

Price 1
Price 2
Price 3

When alternatively, I would like for it to display like this:

Method 1 – $Price 1
Method 2 – $Price 2
Method 3 – $Price 3

I understand why it is displaying this way, but I was curious how I could iterate the loops at the same time and format them, rather than one after the other.

This is my code so far:

add_action( 'woocommerce_admin_order_data_after_shipping_address', 'action_woocommerce_admin_order_data_after_shipping_address', 10, 1 );
function action_woocommerce_admin_order_data_after_shipping_address( $order ){
    // Get meta
    $rate_labels = $order->get_meta( '_available_shipping_methods' );
    $rate_costs = $order->get_meta( '_available_shipping_method_cost' );
    
    $methods = array ( $rate_labels, $rate_costs );
    
    // True
    if ( $rate_labels ) {
        // Loop
        echo '<p><strong>Shipping Methods: </strong>';
        foreach( $rate_labels as $rate_label ) {
            // Output
            echo '<p>' . $rate_label . '</p>';
        }
        foreach( $rate_costs as $rate_cost ) {
            // Output
            echo '<p> $' . $rate_cost . '</p>';
        }
    }
}

2

Answers


  1. Chosen as BEST ANSWER

    In case anyone happens to have the same question as I did, here is how I did it:

    function action_woocommerce_admin_order_data_after_shipping_address( $order ) {
        // Get meta
        $rate_labels = $order->get_meta( '_available_shipping_methods' );
        $rate_costs = $order->get_meta( '_available_shipping_method_cost' );
        
        $methods = array ( $rate_labels, $rate_costs );
        
        // True
        if ( $rate_labels ) {
            // Loop
            echo '<p><strong>Shipping Methods: </strong>';
            foreach(array_combine($rate_labels, $rate_costs) as $rate_label => $rate_cost) {
                 echo '<p>' . $rate_label . ' - $' . $rate_cost . '</p>';
            }
        }
        
    }
    

  2. The following slight different code will display the label and the cost of all available shipping methods (in one array | one foreach loop):

    add_action( 'woocommerce_checkout_create_order', 'action_wc_checkout_create_order' );
    function action_wc_checkout_create_order( $order ) {
        $shipping_data = array(); // Initializing
    
        // Get shipping packages keys from cart
        $packages_keys = (array) array_keys(WC()->cart->get_shipping_packages());
    
        // Loop through shipping packages keys (when cart is split into many shipping packages)
        foreach( $packages_keys as $key ){
            // Get available shipping rates from WC_Session
            $shipping_rates = WC()->session->get('shipping_for_package_'.$key)['rates'];
    
            // Loop through shipping rates
            foreach( $shipping_rates as $rate_key => $rate ){
                // Set all related shipping rate data in the array
                $shipping_data[] = array(
                    'id'          => $rate_key,
                    'method_id'   => $rate->method_id,
                    'instance_id' => (int) $rate->instance_id,
                    'label'       => $rate->label,
                    'cost'        => (float) $rate->cost,
                    'taxes'       => (array) $rate->taxes,
                    'package_key' => (int) $key,
                );
            }
        }
    
        // Save shipping data as order custom field
        if( ! empty($shipping_data) ) {
            $order->update_meta_data( '_shipping_data', $shipping_data );
        }
    }
    
    add_action( 'woocommerce_admin_order_data_after_shipping_address', 'available_shipping_rates_after_shipping_address' );
    function available_shipping_rates_after_shipping_address( $order ) {
        // Get shipping rates custom meta data
        $shipping_data = $order->get_meta( '_shipping_data' );
    
        if ( ! empty($shipping_data) ) {
            echo '<p><strong>Shipping Methods: </strong><br>';
    
            // Loop through shipping rates data
            foreach( $shipping_data as $rate ) {
                // Calculate cost with taxes
                $rate_cost = $rate['cost'] + array_sum($rate['taxes']);
    
                // Output
                echo $rate['label'] . ( $rate_cost > 0 ? ': '. wc_price($rate_cost) : '' ) . '<br>';
            }
    
            echo '</p>';
        }
    }
    

    Code goes in functions.php file of your active child theme (or active theme). Tested and works.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search