Title: Manipulate shipping by passing custom field / trigger
Last modified: November 24, 2016

---

# Manipulate shipping by passing custom field / trigger

 *  [dailce](https://wordpress.org/support/users/dailce/)
 * (@dailce)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/pass-custom/)
 * I can’t seem to get this to work.
 * What I’m trying to do is have a checkbox, when a user clicks it, then offer free
   shipping (and hide all other shipping rates).
 * My code works when I refresh the page, and it works as needed:
 *     ```
       function my_hide_shipping_when_free_is_available( $rates ) {
   
           $free = array();
   
           if ( isset( $_POST['post_data'] ) ) {
               parse_str( $_POST['post_data'], $post_data );
           } else {
               $post_data = $_POST; // fallback for final checkout (non-ajax)
           }
   
       	foreach ( $rates as $rate_id => $rate ) {
       		if ( 'free_shipping' === $rate->method_id && isset($post_data['customorderfield']) && $post_data['customorderfield'] == 1) {
       			$free[ $rate_id ] = $rate;
       			break;
       		}
       	}
   
           if ( isset( $rates['free_shipping:10'] ) ) {
               unset( $rates['free_shipping:10'] );
           }   
   
       	return ! empty( $free ) ? $free : $rates;
       }
       add_filter( 'woocommerce_package_rates', 'my_hide_shipping_when_free_is_available' );
       ```
   
 * However, what I’m trying to do is have shipping update by triggering update_checkout.
   
   So I have added this js to trigger a cart update it does work and sends the post
   field
 *     ```
       add_action( 'wp_footer', 'woocommerce_sendthis_js' );
       function woocommerce_sendthis_js() {
           if (is_checkout()) {
           ?>
           <script type="text/javascript"> 
           jQuery( document ).ready(function( $ ) {
   
       			    jQuery('#mycheckbox').on( 'change', function () {  
       			       jQuery('body').trigger('update_checkout', { update_shipping_method: true } );
       			    });
           });
           </script>
           <?php
           }
       }
       ```
   
 * I added this snippet of code to my woocommerce fields:
 * `<input id="mycheckbox" class="input-checkbox update_totals_on_change" type="
   checkbox" name="customorderfield" value="1" />`
 * Again, the shipping updates properly when I refresh the page, but won’t update
   upon triggering update_checkout, and I’m not sure why. Any ideas?
    -  This topic was modified 9 years, 7 months ago by [dailce](https://wordpress.org/support/users/dailce/).
    -  This topic was modified 9 years, 7 months ago by [dailce](https://wordpress.org/support/users/dailce/).

The topic ‘Manipulate shipping by passing custom field / trigger’ is closed to new
replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

 * 0 replies
 * 1 participant
 * Last reply from: [dailce](https://wordpress.org/support/users/dailce/)
 * Last activity: [9 years, 7 months ago](https://wordpress.org/support/topic/pass-custom/)
 * Status: not resolved