在Woocommerce订单总计表上显示自定义交货选择 [英] Display custom delivery choice on Woocommerce Order totals table

查看:122
本文介绍了在Woocommerce订单总计表上显示自定义交货选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于"更新费根据Woocommerce结帐单答案中的单选按钮动态地,我已经能够为客户交付选择(带有自定义交付选项选择)添加自定义动态费用.

Based on "Update fee dynamically based on radio buttons in Woocommerce checkout" answer code, I have been able add a custom dynamic fee applied on customer delivery choice (with custom delivery option choices).

这是我根据自己的需要修改的工作代码:

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

// Display fees with zero amount
add_filter( 'woocommerce_get_order_item_totals_excl_free_fees', '__return_false' );

我的问题:在Woocommerce中,如何在订单明细表中显示已接收订单页面,我的帐户查看订单页面以及所有电子邮件通知中的所选交付方式.

My question: In Woocommerce, how I can display the chosen delivery method on order detail table for order received page, My account view order pages and on all email notifications.

所以我希望结果看起来像这样:

So I will like the result to look like this:

继续:设为零Woocommerce订单和电子邮件通知中的费用

推荐答案

已更新:我添加了2个函数,这些函数将抓取所选的交付选项到订单中并将其显示在订单的订单明细表中接收页面,我的帐户查看订单页面以及所有电子邮件通知上

Updated: I have added 2 functions that will grab the selected delivery option to the order and display it in order detail table for order received page, My account view order pages and on all email notifications:

// Save chosen delivery as order custom meta data
add_action('woocommerce_checkout_create_order', 'save_order_delivery_option', 10, 2 );
function save_order_delivery_option(  $order, $data ) {
    if ( isset($_POST['radio_delivery_option']) ) {
        $order->update_meta_data( '_chosen_delivery_method', esc_attr($_POST['radio_delivery_option']) );
    }
}

// Display the chosen delivery information
add_filter( 'woocommerce_get_order_item_totals', 'chosen_delivery_item_order_totals', 10, 3 );
function chosen_delivery_item_order_totals( $total_rows, $order, $tax_display ) {;
    $new_total_rows = [];

    // Loop through Order total lines
    foreach($total_rows as $key => $total ){
        // Get the chosen delivery
        $chosen_delivery = $order->get_meta('_chosen_delivery_method');

        if( ! empty($chosen_delivery) && 'payment_method' === $key ){
            $domain = 'woocommerce';
            $label  = __('Delivery method:', 'woocommerce');
            $value  = 'local_pickup' === $chosen_delivery ? __('Local Pickup', $domain) : __('To Your Doorstep', $domain);
            // Display 'Delivery method' line
            $new_total_rows['chosen_delivery'] = array( 'label' => $label,'value' => $value );
        }
        $new_total_rows[$key] = $total;
    }

    return $new_total_rows;
}

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

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

这篇关于在Woocommerce订单总计表上显示自定义交货选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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