Dalton
Dalton

Reputation: 59

Display ALL available shipping methods for each specific order on admin edit order pages in Woocommerce

On the checkout page of my WooCommerce based site, users will have a list of shipping methods to choose from depending on what they are purchasing

This is the list I want to capture:

This is the list I want to capture


Things like

My goal, is to display ALL available methods for each specific order, and display it on the “Edit Order / Order Details” page in the Admin view.

A small feature that would help us be able to quickly identify what option people are choosing more often, depending on the choices that they have available.

This is what I have so far:

add_action( 'woocommerce_checkout_update_order_meta', 'save_available_shipping_methods' );
 
function save_available_shipping_methods( $order_id ) {
    $shippingmethods = WC()->cart->get_shipping_methods();
    update_post_meta( $order_id, '_shipping_methods', $shippingmethods );
}

add_action( 'woocommerce_admin_order_data_after_shipping_address', 'get_available_shipping_methods', 10, 1 );

function get_available_shipping_methods($order){
    $order = wc_get_order( $order_id );
    if ( $order ) {
        echo '<p><strong>'.__('Available Shipping Methods: ').'</strong> ' . get_post_meta($order->get_shipping_methods(), '_shipping_field_value', true ) . '</p>';
    }
}

I'll attach a picture as well to maybe make things a little easier to follow.

This is where I would like to put it. The current hook places the field underneath the shipping address

This is where I would like to put it. The current hook places the field underneath the shipping address

Upvotes: 0

Views: 1788

Answers (2)

Dalton
Dalton

Reputation: 59

In case anyone is wondering, this is the final result. It will loop through each order to find the available shipping methods at the time, along with how much it was quoted for.

 // Capture the available shipping methods, and costs: 
      function action_woocommerce_checkout_update_order_meta( $order_id ) {
      // Get shipping packages
         $packages = WC()->shipping()->get_packages();
        
    // Set array
    $rate_labels = array();
    $rate_costs = array();
    
    // Loop trough packages
    foreach ( $packages as $key => $package ) {
        // Loop through package rates
        foreach( $package['rates'] as $rate_id => $rate ) {
            // Push to array
            $rate_labels[] = $rate->get_label();
            $rate_costs[] = $rate->get_cost();
        }
    }

    // NOT empty
    if ( ! empty ( $rate_labels ) ) {
        // Update post meta
        update_post_meta( $order_id, '_available_shipping_methods', $rate_labels );
        update_post_meta( $order_id, '_available_shipping_method_cost', $rate_costs );
    }
}
add_action( 'woocommerce_checkout_update_order_meta', 'action_woocommerce_checkout_update_order_meta', 10, 1 ); 

// Make it display on the edit order page:
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>';
        }
    }
    
}
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'action_woocommerce_admin_order_data_after_shipping_address', 10, 1 );

Upvotes: 1

7uc1f3r
7uc1f3r

Reputation: 29614

  • In the woocommerce_checkout_update_order_meta hook, the available shipping packages are stored via update_post_meta
  • Comments with explanation added in the code
function action_woocommerce_checkout_update_order_meta( $order_id ) {
    // Get shipping packages
    $packages = WC()->shipping()->get_packages();
    
    // Set array
    $rate_labels = array();
    
    // Loop trough packages
    foreach ( $packages as $key => $package ) {
        // Loop through package rates
        foreach( $package['rates'] as $rate_id => $rate ) {
            // Push to array
            $rate_labels[] = $rate->get_label();
        }
    }

    // NOT empty
    if ( ! empty ( $rate_labels ) ) {
        // Update post meta
        update_post_meta( $order_id, '_available_shipping_methods', $rate_labels );
    }
}
add_action( 'woocommerce_checkout_update_order_meta', 'action_woocommerce_checkout_update_order_meta', 10, 1 ); 

// Display on the order edit page (backend)
function action_woocommerce_admin_order_data_after_shipping_address( $order ) {
    // Get meta
    $rate_labels = $order->get_meta( '_available_shipping_methods' );
    
    // True
    if ( $rate_labels ) {
        // Loop trough rate labels
        foreach( $rate_labels as $rate_label ) {
            // Output
            echo '<p>' . $rate_label . '</p>';
        }
    }
}
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'action_woocommerce_admin_order_data_after_shipping_address', 10, 1 );

Upvotes: 2

Related Questions