Woocommerce custom checkout to add ajax order fee

I am trying to declare a custom fee in the amount of the order at checkout. I added a checkbox in woocommerce

add_action( 'woocommerce_after_checkout_billing_form', 'add_box_option_to_checkout' ); function add_box_option_to_checkout( $checkout ) { echo '<div id="message_fields">'; woocommerce_form_field( 'add_gift_box', array( 'type' => 'checkbox', 'class' => array('add_gift_box form-row-wide'), 'label' => __('Ilość pudełek ozdobnych - 25 PLN/szt'), 'placeholder' => __(''), ), $checkout->get_value( 'add_gift_box' )); } 

Includes a custom js file that handles the event

 jQuery( document ).ready(function( $ ) { $('#add_gift_box').click(function(){ var data = { action: 'woocommerce_add_gift_box', state: '200', }; jQuery.ajax({ type: 'POST', url: wc_checkout_params.ajax_url, data: data, success: function (code) { console.log(code); jQuery('body').trigger('update_checkout'); }, dataType: 'html' }); }); }); 

And php payment processing function

 function woo_add_cart_fee( $data ){ if ( is_admin() && ! defined( 'DOING_AJAX' ) || ! $_POST ) return; $extracost = 0; if (isset($_POST['state'])) { $extracost = intval($_POST['state']); } WC()->cart->add_fee( 'Ozdobne pudełka:', $extracost ); } add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' ); add_action('wp_ajax_woocommerce_add_gift_box', 'woo_add_cart_fee', 10); add_action('wp_ajax_nopriv_woocommerce_add_gift_box', 'woo_add_cart_fee', 10); 

For some reason, the value of $ _POST ['state'] has not been added, the function works when I give a hard coded value, I tried many options, but I can not make it work.

I saw similar notes, but none of them answered.

+7
jquery ajax php wordpress woocommerce
source share
2 answers

Mail data is sent by AJAX functions to 'post_data' serialized. Therefore, to get the value of your flag, you only need parse_str() this!

 parse_str( $_POST['post_data'], $post_data ); 

then you can get your option 'add_gift_box' from $post_data['add_gift_box'] . Please note that after the order is completed, this post_data element is no longer available and everything is in $_POST .

Fill in the example based on your code:


1) adding a checkbox to the checkout

 add_action( 'woocommerce_after_checkout_billing_form', 'add_box_option_to_checkout' ); function add_box_option_to_checkout( $checkout ) { echo '<div id="message_fields">'; woocommerce_form_field( 'add_gift_box', array( 'type' => 'checkbox', 'class' => array('add_gift_box form-row-wide'), 'label' => __('Ilość pudełek ozdobnych - 25 PLN/szt'), 'placeholder' => __(''), ), $checkout->get_value( 'add_gift_box' )); echo '</div>'; } 

2) a script to update the basket when you click on the checkmark (there is no need for additional AJAX requests!)

 add_action( 'wp_footer', 'woocommerce_add_gift_box' ); function woocommerce_add_gift_box() { if (is_checkout()) { ?> <script type="text/javascript"> jQuery( document ).ready(function( $ ) { $('#add_gift_box').click(function(){ jQuery('body').trigger('update_checkout'); }); }); </script> <?php } } 

3) action to add a fee

 add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' ); function woo_add_cart_fee( $cart ){ if ( ! $_POST || ( is_admin() && ! is_ajax() ) ) { return; } if ( isset( $_POST['post_data'] ) ) { parse_str( $_POST['post_data'], $post_data ); } else { $post_data = $_POST; // fallback for final checkout (non-ajax) } if (isset($post_data['add_gift_box'])) { $extracost = 25; // not sure why you used intval($_POST['state']) ? WC()->cart->add_fee( 'Ozdobne pudełka:', $extracost ); } } 
+15
source share

This is amazing!! Many thanks. I changed it a bit to add a percentage. I know this is not the best answer, but I have no reputation to raise your answer. For those who are stuck like me ..

 add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' ); function woo_add_cart_fee( $cart ){ global $woocommerce; if ( ! $_POST || ( is_admin() && ! is_ajax() ) ) { return; } if ( isset( $_POST['post_data'] ) ) { parse_str( $_POST['post_data'], $post_data ); } else { $post_data = $_POST; // fallback for final checkout (non-ajax) } if (isset($post_data['add_gift_box'])) { $percentage = 0.01; $surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage; $woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' ); } } 
+4
source share

All Articles