'WooCommerce shortcode to ajaxify message that display: "You need to spend to get free shipping"

I modified this script to make a shortcode that shows how much the customer need to spend to get free shipping.

 /** 
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.9
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
 
add_shortcode ('woo_freeshipping_target', 'woo_freeship_target' );
  
function woo_freeship_target() {
  ob_start();
    
   $min_amount = 279; //change this to your free shipping threshold
   
   $current = WC()->cart->subtotal;
  
   if ( $current < $min_amount ) {
      $added_text = wc_price( $min_amount - $current );
      $notice = sprintf( $added_text );
      echo '<span> You need to add '.$notice.'</span>';
   }
    
     else if ( $current >= $min_amount ) {
      echo '<span>Free Shipping!</span>';
   }
  
    return ob_get_clean();
    
}

But I cannot figure out how can I make it to auto update when the items quantity is modified. Any idea?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source