Отображение настраиваемых полей в конкретном уведомлении по электронной почте WooCommerce

На моем сайте, основанном на WooCommerce, я недавно добавил код для отображения способов доставки и цен для каждого заказа на странице редактирования заказа. Теперь я хотел бы попробовать и добавить те же поля в шаблон электронной почты «Новый заказ», который отправляется администратору. Вот что у меня есть на данный момент:

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

В дополнение к этому, это то, что я пытался заставить работать, но пока безуспешно:

// Add it to the new order email template 
add_filter( 'woocommerce_new_order', 'custom_woocommerce_email_order_meta_fields', 10, 3 );

function custom_woocommerce_email_order_meta_fields( $rate_labels, $sent_to_admin, $order ) {
    
    $rate_labels = $order->get_meta( '_available_shipping_methods' );
    $rate_costs = $order->get_meta( '_available_shipping_method_cost' );
    
    $methods = array ( $rate_labels, $rate_costs );
    
    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>';
        }
    }
}

person Dalton    schedule 15.07.2020    source источник


Ответы (1)


Вместо этого используйте, например, следующее:

add_action( 'woocommerce_email_after_order_table', 'wc_email_new_order_custom_meta_data', 10, 4);
function wc_email_new_order_custom_meta_data( $order, $sent_to_admin, $plain_text, $email ){
    // On "new order" email notifications
    if ( 'new_order' === $email->id ){
        $rate_labels = $order->get_meta( '_available_shipping_methods' );
        $rate_costs = $order->get_meta( '_available_shipping_method_cost' );
        
        $methods = array ( $rate_labels, $rate_costs );
        
        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>';
            }
        }
    }
}

Он должен работать.

person LoicTheAztec    schedule 15.07.2020
comment
Нашел аналогичное решение, но это тоже сработало. Спасибо! - person Dalton; 20.07.2020