Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
138 views
in Technique[技术] by (71.8m points)

php - Display all available shipping methods and costs on WooCommerce Order pages

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>';
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...