Отображение ВСЕХ доступных способов доставки для каждого указанного c заказа на страницах редактирования заказа администратора в Woocommerce - PullRequest
0 голосов
/ 08 июля 2020

На странице оформления заказа моего сайта WooCommerce пользователи будут иметь список способов доставки на выбор в зависимости от того, что они покупают

Это список, который я хочу захватить:

This is the list I want to capture


Things like

  • “Free Shipping” for orders over a certain price
  • “Freight Shipping” for certain items, and so on.

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

Я также прикреплю картинку, чтобы, может быть, было немного легче следить.

Вот где я бы хотел ее поместить. Текущая ловушка помещает поле под адресом доставки

Вот где я хотел бы это поставить. Текущая ловушка помещает поле под адресом доставки

Ответы [ 2 ]

3 голосов
/ 08 июля 2020
  • В хуке woocommerce_checkout_update_order_meta доступные отгрузочные пакеты хранятся через update_post_meta
  • Комментарии с пояснением, добавленными в код
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 );
0 голосов
/ 09 июля 2020

Если кому интересно, вот конечный результат. Он будет oop через каждый заказ, чтобы найти доступные способы доставки на тот момент, а также то, по какой цене он был указан.

 // 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 );
...