【问题标题】:Display custom delivery choice on Woocommerce Order totals table在 Woocommerce 订单总计表上显示自定义交付选项
【发布时间】:2019-07-13 10:49:09
【问题描述】:

基于“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>';
}

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

我的问题:在 Woocommerce 中,我如何在订单详细信息表中的订单接收页面、我的帐户查看订单页面和所有电子邮件通知中显示所选的交付方式。

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

续:Make appear a zero fee in Woocommerce Orders and email notifications

【问题讨论】:

    标签: php ajax woocommerce orders fee


    【解决方案1】:

    更新:我添加了 2 个功能,它们将获取订单中选定的交付选项并将其显示在订单详细信息表中的订单接收页面、我的帐户查看订单页面和所有电子邮件通知中:

    // 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 文件中。经过测试并且可以工作。

    【讨论】:

    • 由于某种原因,在我使用您的上述代码(仅显示本地取货)后,To Your Doorstep Delivery 选项未显示,并且我收到一条错误消息,指出变量 $delivery 未在行 @987654325 上定义@
    • @KolawoleEmmanuelIzzy 我刚刚保留了最后两个挂钩函数(进行必要的更改),它们将与我们的代码一起使用。试试看……它应该可以工作。
    • 我追溯了我的脚步,它工作正常......不知道为什么我在第一次测试时得到了未定义的 $delivery。我认为这是因为我没有在同一个文件上添加 ajax 函数,但是当我完全按原样复制所有代码时,出现了 To Your Doorstep 选项。然后使用您的所有代码,我从同一页面中删除了 Ajax 函数并复制到我为所有 ajax 函数创建的文件中,并且我没有收到任何未定义的错误。不知道我以前做了什么不同的事情。现在一切正常。非常感谢
    • 我看到您已经删除了其他代码并仅添加了两个新代码,如果将来有人遇到相关问题,将很高兴拥有完整的代码。感谢@LoicTheAztec 的出色工作。现在我需要弄清楚当附加的订单被标记为已完成时自动删除产品,如之前要求的here。将感谢或帮助或想法我如何能做到这一点。
    • @KolawoleEmmanuelIzzy 是的!在电子邮件表上也显示订单行总数......我将在此答案的后面添加所有重新访问的所需代码。
    猜你喜欢
    • 2020-11-11
    • 1970-01-01
    • 2021-11-20
    • 2021-06-10
    • 2021-07-24
    • 1970-01-01
    • 2018-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多