【问题标题】:Force display a zero fee in Woocommerce Orders and email notifications强制在 Woocommerce 订单和电子邮件通知中显示零费用
【发布时间】:2019-07-12 09:39:23
【问题描述】:

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

现在的问题是,当客户选择Local Pickup送货方式自定义单选按钮时,应用的费用等于零根本不显示收到订单页面、我的帐户查看订单页面和所有电子邮件通知。

那么如何获得显示(在订单总额表上)就像正常费用一样:

所以显示的总项目行将类似于:

运费:N0.00

【问题讨论】:

    标签: php ajax woocommerce orders fee


    【解决方案1】:

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

    但您可以使用这行简单的代码,让它随处显示为大于零的费用:

    add_filter( 'woocommerce_get_order_item_totals_excl_free_fees', '__return_false' );
    

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

    【讨论】:

    • 像魅力一样在订单表上显示具有零(0)费用的交付方式。谢谢@LoicTheAztec。但仍然不确定如何在订单详细信息上方显示标签,如问题所附图像中所示。您发出了关于在其他地方提出另一个问题以获得交付方式的答案的通知,我已经提出了问题here
    • 非常有用。像魅力一样工作。
    • @LoicTheAztec 我们可以显示“免费”而不是 0.00 吗?
    【解决方案2】:

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

    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>
    

    【讨论】:

    • 当我在感谢页面上使用时返回数组,当我 print_r (get_post_meta($order-&gt;get_id(), 'chosen_delivery_option')); ...我在想要显示所选选项的位置得到 array(0) { }
    • @KolawoleEmmanuelIzzy 您可能需要使用您正在使用的密钥来保存这些详细信息。
    猜你喜欢
    • 2018-06-07
    • 2021-05-05
    • 1970-01-01
    • 2019-08-27
    • 1970-01-01
    • 2021-09-05
    • 2020-12-06
    • 2021-04-19
    • 2018-06-04
    相关资源
    最近更新 更多