猿问

在 woocommerce_review_order_before_submit 中显示自定义字段

我有一个使用以下代码创建的自定义结帐字段:


add_action( 'woocommerce_before_order_notes', 'add_custom_checkout_field' );

function add_custom_checkout_field( $checkout ){

  woocommerce_form_field( 'number_id', array(        

    'type' => 'number',        

    'class' => array( 'form-row-wide' ),        

    'label' => 'Amount',        

    'placeholder' => 'Place your number here',        

    'required' => false,              

   ),  $checkout->get_value( 'number_id' ) ); 

}

我希望此字段显示在放置订单按钮之前的空间中。我尝试执行以下操作,但没有成功:


    add_action( 'woocommerce_review_order_before_submit', 'review_order_before_submit_state_message' );

    function review_order_before_submit_state_message() {

        $amount = WC()->session->get('number_id');

        $message = "Amount you entered is: <strong>".$amount;

            echo '<ul class="woocommerce-info">'.$message.'</ul>';

    }

这个数量也可以根据客户的输入而改变,所以也尝试了 javascript,但购物车没有刷新:


add_action( 'wp_footer', 'bbloomer_checkout_radio_choice_refresh' );


function bbloomer_checkout_radio_choice_refresh() {

if ( ! is_checkout() ) return;

?>

<script type="text/javascript">

jQuery( function($){

    $('form.checkout').on('change', 'input[name=number_id]', function(e){

        e.preventDefault();

        var p = $(this).val();

        $.ajax({

            type: 'POST',

            url: wc_checkout_params.ajax_url,

            data: {

                'action': 'woo_get_ajax_data',

                'radio': p,

            },

            success: function (result) {

                $('body').trigger('update_checkout');

            }

        });

    });

});

</script>

<?php

}

// WP Ajax Function

add_action( 'wp_ajax_woo_get_ajax_data', 'woo_get_ajax_data' ); 

add_action( 'wp_ajax_nopriv_woo_get_ajax_data', 'woo_get_ajax_data' ); 

function woo_get_ajax_data() { 

    if ( isset($_POST['number_id']) ){ 

        $number_id = sanitize_key( $_POST['number_id'] ); 

        WC()->session->set('number_id', $number_id ); 

        echo json_encode( $number_id ); 

    } 

    die(); 

谁能告诉我我做错了什么?谢谢你。

烙印99
浏览 66回答 1
1回答

饮歌长啸

我已经解决了这个问题。感谢大家的贡献:&nbsp; &nbsp; /*&nbsp; &nbsp; This code adds a custom field in shipping on checkout that lets the customer add their amount which gets displayed in the review order box*/// Add a custom input field to be displayed on checkoutadd_action( 'woocommerce_before_checkout_shipping_form', 'checkout_shipping_form_cod_addition', 20 );function checkout_shipping_form_cod_addition( ) {&nbsp; &nbsp; $domain = 'woocommerce';&nbsp; &nbsp; $customer_cod&nbsp; &nbsp;= WC()->session->get('cod_input');&nbsp; &nbsp; // Add a custom input number field&nbsp; &nbsp; woocommerce_form_field( 'cod_input', array(&nbsp; &nbsp; &nbsp; &nbsp; 'type'&nbsp; &nbsp; &nbsp; => 'number',&nbsp; &nbsp; &nbsp; &nbsp; 'class'&nbsp; &nbsp; &nbsp;=> array( 'form-row-wide' ),&nbsp; &nbsp; &nbsp; &nbsp; 'label' => 'Amount to be charged to customers',&nbsp; &nbsp; &nbsp; &nbsp; 'placeholder' => 'COD',&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 'required'&nbsp; => false,&nbsp; &nbsp; ), $customer_cod );}// jQuery - Ajax script - to refresh checkoutadd_action( 'wp_footer', 'checkout_shipping_cod_script' );function checkout_shipping_cod_script() {&nbsp; &nbsp; // Only checkout page&nbsp; &nbsp; //if ( is_checkout() && ! is_wc_endpoint_url() ) :if ( ! is_checkout() ) return;&nbsp; &nbsp; WC()->session->__unset('cod_input');&nbsp; &nbsp; ?>&nbsp; &nbsp; <script type="text/javascript">&nbsp; &nbsp; jQuery( function($){&nbsp; &nbsp; &nbsp; &nbsp; $('form.checkout').on('change', 'input#cod_input', function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var p = $(this).val();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(p);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: 'POST',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: wc_checkout_params.ajax_url,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'action': 'woo_get_ajax_data',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'cod': p,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function (result) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('body').trigger('update_checkout');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; });&nbsp; &nbsp; </script>&nbsp; &nbsp; <?php&nbsp; &nbsp; //endif;}// Php Ajax (Receiving request and saving to WC session)add_action( 'wp_ajax_woo_get_ajax_data', 'woo_get_ajax_data' );add_action( 'wp_ajax_nopriv_woo_get_ajax_data', 'woo_get_ajax_data' );function woo_get_ajax_data() {&nbsp; &nbsp; if ( isset($_POST['cod']) ){&nbsp; &nbsp; &nbsp; &nbsp; $cod = sanitize_key( $_POST['cod'] );&nbsp; &nbsp; &nbsp; &nbsp; WC()->session->set('cod_input', $cod );&nbsp; &nbsp; &nbsp; &nbsp; echo json_encode( $cod );&nbsp; &nbsp; }&nbsp; &nbsp; die(); // Alway at the end (to avoid server error 500)}//Add the output to the review order boxadd_action( 'woocommerce_review_order_before_submit', 'review_order_before_submit_state_message' );function review_order_before_submit_state_message() {&nbsp; &nbsp;global $woocommerce;&nbsp; &nbsp; $customer_cod = WC()->session->get( 'cod_input' ); // Dynamic cod&nbsp; &nbsp; $total_amount = floatval( preg_replace( '#[^\d.]#', '', WC()->cart->get_cart_contents_total() ) );&nbsp; &nbsp; $shipping = floatval( preg_replace( '#[^\d.]#', '', WC()->cart->get_shipping_total( )) );&nbsp; &nbsp; $profit = $customer_cod-$total_amount-$shipping;&nbsp; &nbsp;$cod_message = "COD amount is: <strong>Rs. ".$customer_cod."</strong>";&nbsp; &nbsp;$profit_message = "Your Profit is: <strong>Rs. ".$profit."</strong>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ( !empty( $customer_cod) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo '<ul class="cod-info">'.'<li>'.$cod_message.'</li>'.'<li>'.$profit_message.'</li>'.'</ul>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}}//saves the new checkout field&nbsp; &nbsp; add_action( 'woocommerce_checkout_update_order_meta', 'save_cod_amount' );&nbsp; &nbsp; function save_cod_amount( $order_id ) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if ( $_POST['cod_input'] ) update_post_meta( $order_id, 'cod_input', esc_attr( $_POST['cod_input'] ) );&nbsp; &nbsp; }//adds the new checkout field value to the admin order that can be edited&nbsp; &nbsp; add_action( 'woocommerce_admin_order_data_after_shipping_address', 'show_new_checkout_field_in_shopping_order', 10, 1 );&nbsp; &nbsp; function show_new_checkout_field_in_shopping_order( $order ) {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $order_id = $order->get_id();&nbsp; &nbsp; $cod_input = get_post_meta( $order->id, 'cod_input', true );?><div class="address">&nbsp; &nbsp; <p<?php if( empty($cod_input) ) echo ' class="none_set"' ?>>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<strong>COD Amount:</strong>&nbsp; &nbsp; &nbsp; &nbsp; <?php echo ( !empty( $cod_input ) ) ? $cod_input : '-' ?>&nbsp; &nbsp; </p></div><div class="edit_address"><?php&nbsp; &nbsp; woocommerce_wp_text_input( array(&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 'id' => 'cod_input',&nbsp; &nbsp; &nbsp; &nbsp; 'label' => 'COD amount',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 'wrapper_class' => 'form-field-wide',&nbsp; &nbsp; &nbsp; &nbsp; 'value' => $cod_input,) );?></div>&nbsp; &nbsp; <?php&nbsp; &nbsp; }//if this field is altered then it needs to be saved&nbsp; &nbsp; add_action( 'woocommerce_process_shop_order_meta', 'save_cod_input' );&nbsp; &nbsp; function save_cod_input( $ord_id ){&nbsp; &nbsp; &nbsp; &nbsp; update_post_meta( $ord_id, 'cod_input', wc_clean( $_POST[ 'cod_input' ] ) );&nbsp; &nbsp; }/* End of this code */&nbsp;&nbsp;
随时随地看视频慕课网APP
我要回答