Display all available shipping methods and costs on WooCommerce Order pages

风流意气都作罢 提交于 2020-07-10 10:24:19

问题


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

I am using Display ALL available shipping methods for each specific order on admin edit order pages in Woocommerce answer code to my previous question.

It does not displaythe 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

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>';
        }
    }
}

回答1:


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.




回答2:


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>';
        }
    }
    
}


来源:https://stackoverflow.com/questions/62819259/display-all-available-shipping-methods-and-costs-on-woocommerce-order-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!