在Woocommerce订单和电子邮件通知中强制显示零费用 [英] Force display a zero fee in Woocommerce Orders and email notifications

查看:114
本文介绍了在Woocommerce订单和电子邮件通知中强制显示零费用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Woocommerce中,我试图显示自定义费用,当费用金额为零时,以使其显示在订单明细表中.

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.

基于"更新费动态地基于Woocommerce结帐中的单选按钮答案代码, 我设法添加了适用于购物车的动态费用,并更改了客户的送货选择.

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:

因此显示的总计项目行将类似于:

So the displayed total item line will be something like:

送货费: N0.00

推荐答案

零费用情况:如您所知,当费用等于零时,它不会出现在woocommerce订单中和电子邮件通知.

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

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.

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

这篇关于在Woocommerce订单和电子邮件通知中强制显示零费用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆