2

In Woocommerce, I am trying to get the display of a custom fee, when the fee amount is zero, to make it appear in the order detail table.

Based on "Update fee dynamically based on radio buttons in Woocommerce checkout" answer code, I have manage to add a dynamic fee that is applied to cart and changes on customer delivery choice.

Here is the working code that I have adapted for my needs:

add_action( 'woocommerce_cart_calculate_fees', 'add_delivery_fee', 20, 1 );
function add_delivery_fee( $cart ) {
    $domain = 'woocommerce';
    $NipostFST = '2000';
    $NIPOSTSUB = '250';

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $packing_fee = WC()->session->get( 'chosen_delivery_option' ); // Dynamic delivery fee
    $Dlabel = $packing_fee == 'home_delivery' ? __('To Your Doorstep', $domain) : __('Local Pickup', $domain);
    $weight_of_item = WC()->cart->cart_contents_weight;

    if ( $weight_of_item > 1 ) {
        $nipostFee = $NipostFST + ( ($weight_of_item - 1)*$NIPOSTSUB );
    }elseif ( $weight_of_item == 1 ) {
        $nipostFee = $NipostFST;
    }

    $fee = (isset($weight_of_item)) ? $packing_fee == 'home_delivery' ? $nipostFee : 0.00 : 'Not Available';
    $cart->add_fee( !is_cart() ? __( 'Delivery Fee [ '.$Dlabel.' ]', $domain ) : __( 'Delivery Fee', $domain ), $fee );
}

// Add a custom radio fields for Delivery Option selection
add_action( 'woocommerce_checkout_before_order_review', 'checkout_delivery_fee_addition', 20 );
function checkout_delivery_fee_addition(){
    $domain       = 'woocommerce';
    $weight_of_item = WC()->cart->cart_contents_weight;

    $NipostFST = '2000';
    $NIPOSTSUB = '250';


    if ( $weight_of_item > 1 ) {
        $nipostFee = $NipostFST + ( ($weight_of_item - 1)*$NIPOSTSUB );
    }elseif ( $weight_of_item == 1 ) {
        $nipostFee = $NipostFST;
    }

    echo '<div id="izzycart_checkout_addons"><tr class="deliveryOption-select"><th>' . __('Delivery Method', $domain) . '</th><td>';

    $chosen   = WC()->session->get('chosen_delivery_option');
    $chosen   = empty($chosen) ? WC()->checkout->get_value('radio_delivery_option') : $chosen;
    $chosen   = empty($chosen) ? 'local_pickup' : $chosen;

    // Add a custom checkbox field
    woocommerce_form_field( 'radio_delivery_option', array(
        'type' => 'radio',
        'class' => array( 'form-row-wide delivery_option' ),
        'options' => array(
            'local_pickup' => __('Local Pickup '.wc_price(0.00), $domain),
            'home_delivery' => (!isset($weight_of_item)) ? __('To Your Doorstep <br><span style="color:red">Not Available</span>', $domain) : __('To Your Doorstep '.wc_price($nipostFee), $domain),
        ),
        'default' => $chosen,
    ), $chosen );

    echo '</td></tr></div>';
}

Now, the problem is that, when customer choose Local Pickup delivery method custom radio button, the applied fee is equal to zero and not displayed at all on order received page, on My account view order page and on all email notifications.

So how to get the display (on orders totals table) just like a normal fee that should look like:

enter image description here

So the displayed total item line will be something like:

Delivery Fee: N0.00

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

2 Answers2

3

The zero fee case: As you already know, when a fee is equal to zero, it doesn't appear in woocommerce orders and email notifications.

But you can use this simple line of code, to make it appear everywhere as fees bigger than zero:

add_filter( 'woocommerce_get_order_item_totals_excl_free_fees', '__return_false' );

Code goes in function.php file of your active child theme (or active theme). Tested and works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Works like charm to display the delivery method having Zero(0) fee on the order table. Thanks @LoicTheAztec. But still not sure how to display the label above the order details like in the image attached to the question. You gave a notice regarding asking another question in other to get answer for the Delivery Method, i have asked the question [here](https://stackoverflow.com/questions/54774616/display-custom-woocommerce-checkout-radio-button-selection-on-order-detail-table) – Kolawole Emmanuel Izzy Feb 19 '19 at 20:44
  • Very useful. Works like a charm. – Khoa Oct 19 '20 at 00:22
  • @LoicTheAztec can we display "Free" instead of 0.00? – VijayRana Aug 04 '21 at 07:12
0

There are two hooks to modify the content above or below of order details table.

woocommerce_email_before_order_table

woocommerce_email_after_order_table

If you want to modify the table itself - Copy below file to your theme woocommerce/templates/emails/email-order-details.php . and one more row

            <?php  $deliverfee = get_post_meta($order->get_id(), 'chosen_delivery_option');
            <tr>
                <th class="td" scope="row" colspan="2" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php esc_html_e( 'Delivery Fee:', 'woocommerce' ); ?></th>
                <td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php echo  $deliverfee ; ?></td>
            </tr>
mujuonly
  • 11,370
  • 5
  • 45
  • 75
  • This returned Array when i used on the thankyou page and when i `print_r (get_post_meta($order->get_id(), 'chosen_delivery_option'));` ... i got `array(0) { }` in the location want to to display the selected option – Kolawole Emmanuel Izzy Feb 19 '19 at 20:46
  • @KolawoleEmmanuelIzzy You may need to use the key that you are using to save those details. – mujuonly Feb 20 '19 at 04:27