Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在WooCommerce签出中的订单总额之前添加交货单选按钮_Php_Wordpress_Woocommerce_Checkout_Hook Woocommerce - Fatal编程技术网

Php 在WooCommerce签出中的订单总额之前添加交货单选按钮

Php 在WooCommerce签出中的订单总额之前添加交货单选按钮,php,wordpress,woocommerce,checkout,hook-woocommerce,Php,Wordpress,Woocommerce,Checkout,Hook Woocommerce,我正在写一个WordPress插件,我需要在WooCommerce订单审查部分的订单总数之前添加两个单选按钮。我知道了如何将自定义单选按钮添加到“订单审阅”部分,但我无法了解如何在订单总计之前移动交付选项 请查看屏幕截图以了解我到底想要实现什么 这是我的密码: // Part 1 - Display Radio Buttons add_action( 'woocommerce_review_order_before_payment', 'custom_checkout_radio_choice

我正在写一个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'] );
    }
}
//第1部分-显示单选按钮
添加操作(“付款前商业审查订单”、“定制结帐”、“无线电选择”);
功能自定义\检查\收音机\选择(){
$selected=WC()->session->get('radio_selected');
$selected=空($selected)?WC()->checkout->get_value('radio_choice'):$selected;
$selected=空($selected)?'0':$selected;
$args=数组(
'类型'=>'无线电',
'class'=>数组('form row wide','update_totals_on_change'),
“选项”=>数组(
“2.95”=>“60分钟:2.95欧元”,
“0”=>“24-48小时”,
),
“默认值”=>$selected
);
回声';
echo“交付选项”;
woocommerce_表单_字段('radio_choice',$args,$selected);
回声';
}
//第2部分-添加费用并计算总费用
添加操作('woocommerce\u cart\u calculate\u fees','custom\u checkout\u radio\u choice\u fee',20,1);
功能自定义\结账\收音机\选择\费用($cart){
if(定义了('DOING'uajax'))
返回;
$radio=WC()->session->get('radio_selected');
中频(无线电){
$cart->add_fee('Delivery fee',$radio);
}
}
//第3部分-将收音机选项添加到会话
添加操作('woocommerce\u checkout\u update\u order\u review'、'custom\u checkout\u radio\u choice\u set\u session');
功能自定义\签出\无线电\选择\设置\会话($posted\数据){
parse_str($posted_data,$output);
if(isset($output['radio_choice'])){
WC()->session->set('radio_choice',$output['radio_choice');
}
}

请帮我解决这个问题。

要在订单总计之前移动单选按钮,您需要使用另一个挂钩。但是你不能在总费用线上有送货单选按钮

我简化并重新查看了代码:

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'] );
    }
}
add_action('woocommerce_review_order_前_order_总计','checkout_delivery_单选按钮');
功能签出\交付\单选按钮(){
回声'
“交付选项”;
$selected=WC()->session->get('delivery');
$selected=空($selected)?WC()->checkout->get_value('delivery'):$selected;
$selected=空($selected)?'0':$selected;
表单字段('delivery',数组)(
'类型'=>'无线电',
'class'=>数组('form row wide','update_totals_on_change'),
“选项”=>数组(
“2.95”=>“60分钟:2.95欧元”,
“0”=>“24-48小时”,
),
),$已选择);
回声';
}
添加操作('woocommerce\u cart\u calculate\u fees','checkout\u delivery\u fee',20,1);
功能签出\交付\费用($cart){
如果($radio=WC()->session->get('delivery')){
$cart->add_fee('Delivery fee',$radio);
}
}
添加操作('woocommerce\u checkout\u update\u order\u review'、'checkout\u delivery\u choice\u to\u session');
功能签出\交付\选择\到\会话($posted\数据){
parse_str($posted_data,$output);
if(isset($output['delivery'])){
WC()->session->set('delivery',$output['delivery']);
}
}

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

您不能将这些选项添加为装运方式吗?并使用WooCommerce中的内置功能?如果没有,或者我遗漏了一些东西,那么一定要检查一下模板,看看这是如何申请费用的。另外,不要忘记这些是表格<代码>表格中使用的代码打印在其上方或下方,因此使用标记作为输出instead@7uc1f3r我是WordPress的新手,我的要求是只使用自定义插件来处理这个问题。这些选项应该在插件停用后立即消失。还有很多,但我被困在这里了。谢谢@7uc1f3r的提示。我在checkout/review-order.php文件上做了一些挖掘,看起来我已经非常接近我的解决方案了。我不知道这是否是一个好的做法,但我非常接近谢谢,伙计。我使用了不同的方法,但你的例子似乎比我的好。非常感谢您的帮助。如果我需要动态数据呢?例如:如果我想获得收音机字段中小计的百分比,我该怎么做?作为一个新问题,在你的问题中提供你自己的真实代码尝试(请注意StackOverFlow不是免费的编码服务)。@LoicTheAztec StackOverFlow不再允许我发布新问题。所以,如果你能回答我的问题,那就太好了。@Rembala用一封不同的电子邮件创建一个新的个人资料……并提出一个新的问题,在你的问题中提供你自己真正的代码尝试。