【问题标题】:Add Delivery Radio Buttons before order total in WooCommerce checkout在 WooCommerce 结帐中的订单总额之前添加交付单选按钮
【发布时间】:2020-10-10 10:58:09
【问题描述】:

我正在编写一个 WordPress 插件,我需要在 WooCommerce 订单审查部分的订单总数之前添加两个单选按钮。我想出了如何将自定义单选按钮添加到订单审核部分,但我不知道如何在订单总数之前移动 交货选项

请查看屏幕截图以了解我到底想要实现什么。

这是我的代码:

// Part 1 - Display Radio Buttons
add_action( 'woocommerce_review_order_before_payment', 'custom_checkout_radio_choice' );
function custom_checkout_radio_choice() {
     
   $chosen = WC()->session->get( 'radio_chosen' );
   $chosen = empty( $chosen ) ? WC()->checkout->get_value( 'radio_choice' ) : $chosen;
   $chosen = empty( $chosen ) ? '0' : $chosen;
        
   $args = array(
       'type' => 'radio',
       'class' => array( 'form-row-wide', 'update_totals_on_change' ),
        'options' => array(
            '2.95' => '60 MINUTES: €2.95',
            '0' => '24 - 48 HOURS',
        ),
        'default' => $chosen
   );
     
   echo '<div id="checkout-radio">';
   echo '<h3>Delivery Options</h3>';

   woocommerce_form_field( 'radio_choice', $args, $chosen );

   echo '</div>'; 
}
  
// Part 2 - Add Fee and Calculate Total
add_action( 'woocommerce_cart_calculate_fees', 'custom_checkout_radio_choice_fee', 20, 1 );
function custom_checkout_radio_choice_fee( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) 
        return;
    
    $radio = WC()->session->get( 'radio_chosen' );

    if ( $radio ) {
        $cart->add_fee( 'Delivery Fee', $radio );
    }
}
  
// Part 3 - Add Radio Choice to Session
add_action( 'woocommerce_checkout_update_order_review', 'custom_checkout_radio_choice_set_session' );
function custom_checkout_radio_choice_set_session( $posted_data ) {
    parse_str( $posted_data, $output );
    if ( isset( $output['radio_choice'] ) ){
        WC()->session->set( 'radio_chosen', $output['radio_choice'] );
    }
}

请帮帮我。

【问题讨论】:

  • 您不能将这些选项添加为运输方式吗?并使用 WooCommerce 中的内置功能?如果没有,或者我遗漏了一些东西,那么请务必查看checkout/review-order.php 模板以及如何申请费用。另外,不要忘记这些是表格。表格中使用的&lt;div&gt; 打印在其上方或下方,因此请使用table 标记代替输出
  • @7uc1f3r 我对 WordPress 很陌生,我要求只使用自定义插件来处理这个问题。停用插件后,这些选项应立即消失。还有很多,但我被困在这里。
  • 感谢@7uc1f3r 提供线索。我对 checkout/review-order.php 文件进行了一些挖掘,看起来我非常接近我的解决方案。我不知道这是否是一个好习惯,但我非常接近。 :)

标签: php wordpress woocommerce checkout hook-woocommerce


【解决方案1】:

要在订单总数之前移动单选按钮,您需要使用另一个挂钩。但是您不能在费用总行上设置交付单选按钮……

我已经简化并重新访问了代码:

add_action( 'woocommerce_review_order_before_order_total', 'checkout_delivery_radio_buttons' );
function checkout_delivery_radio_buttons() {
    echo '<tr class="delivery-radio">
            <th>'.__("Delivery Options").'</th><td>';

    $chosen = WC()->session->get( 'delivery' );
    $chosen = empty( $chosen ) ? WC()->checkout->get_value( 'delivery' ) : $chosen;
    $chosen = empty( $chosen ) ? '0' : $chosen;

    woocommerce_form_field( 'delivery',  array(
        'type'      => 'radio',
        'class'     => array( 'form-row-wide', 'update_totals_on_change' ),
        'options'   => array(
            '2.95'  => '60 MINUTES: €2.95',
            '0'     => '24 - 48 HOURS',
        ),
    ), $chosen );
    
    echo '</td></tr>';
}

add_action( 'woocommerce_cart_calculate_fees', 'checkout_delivery_fee', 20, 1 );
function checkout_delivery_fee( $cart ) {
    if ( $radio = WC()->session->get( 'delivery' ) ) {
        $cart->add_fee( 'Delivery Fee', $radio );
    }
}

add_action( 'woocommerce_checkout_update_order_review', 'checkout_delivery_choice_to_session' );

function checkout_delivery_choice_to_session( $posted_data ) {
    parse_str( $posted_data, $output );
    if ( isset( $output['delivery'] ) ){
        WC()->session->set( 'delivery', $output['delivery'] );
    }
}

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

【讨论】:

  • 谢谢,伙计。我使用了不同的方法,但你的例子似乎比我的好。非常感谢您的帮助。
  • 如果我想要动态数据怎么办?例如:如果我想在单选字段中获取小计的百分比,我该怎么做?
  • 作为一个新问题,在您的问题中提供您自己的真实代码尝试(请注意 StackOverFlow 不是免费的编码服务)
  • @LoicTheAztec Stackoverflow 不再允许我发布新问题。如果您能回答我的问题,那就太好了。
  • @RemanBala 使用不同的电子邮件创建一个新的个人资料......并提出一个新问题,在您的问题中提供您自己的真实代码尝试。
猜你喜欢
  • 2021-06-08
  • 1970-01-01
  • 2021-06-05
  • 2021-07-29
  • 1970-01-01
  • 2021-01-30
  • 2020-10-20
  • 2021-11-16
  • 2023-03-30
相关资源
最近更新 更多