强制在Woocommerce订单和电子邮件通知中显示零费用

问题描述 投票:1回答:2

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

基于“Update fee dynamically based on radio buttons in Woocommerce checkout”答案代码,我已设法添加适用于购物车的动态费用以及客户交付选择的更改。

以下是我根据自己的需求调整的工作代码:

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

现在,问题是,当客户选择本地提货交付方式自定义单选按钮时,应用的费用等于零,并且在订单接收页面,我的帐户查看订单页面和所有电子邮件通知上根本不显示。

那么如何获得显示(在订单总计表上)就像正常的费用应该是这样的:

enter image description here

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

运费:N0.00

php ajax woocommerce orders fee
2个回答
1
投票

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

但是你可以使用这个简单的代码行,使其在任何地方都显示为大于零的费用:

add_filter( 'woocommerce_get_order_item_totals_excl_free_fees', '__return_false' );

代码位于活动子主题(或活动主题)的function.php文件中。经过测试和工作。


0
投票

有两个钩子可以修改订单详细信息表格上方或下方的内容。

woocommerce_email_before_order_table

woocommerce_email_after_order_table

如果您想修改表格本身 - 将下面的文件复制到您的主题woocommerce / templates / emails / email-order-details.php。还有一排

            <?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>
© www.soinside.com 2019 - 2024. All rights reserved.