Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

160
Visualizações
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?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

To ajaxify your message so it updates when the subtotal change (via ajax) use the woocommerce_add_to_cart_fragments filter hook

So you get:

function custom_message() {
    // Initialize
    $message = __( 'Default message', 'woocommerce' );
    
    // True
    if ( WC()->cart ) {
        // Change this to your free shipping threshold
        $min_amount = 279;
        
        // Get cart subtotal
        $subtotal = WC()->cart->get_subtotal();

        if ( $subtotal < $min_amount ) {
            $message = sprintf( __( 'You need to add %s to get free shipping', 'woocommerce' ), wc_price( $min_amount - $subtotal ) );
        } else {
            $message = __( 'Free shipping', 'woocommerce' );
        }
    }
    
    return $message;
}

// Refreshing on cart ajax events
function filter_woocommerce_add_to_cart_fragments( $fragments ) {    
    $fragments['div.display-message'] = '<div class="display-message">' . custom_message() . '</div>';
    
    return $fragments;
}
add_filter( 'woocommerce_add_to_cart_fragments', 'filter_woocommerce_add_to_cart_fragments', 10, 1 );

// Shortcode
function display_my_message() {
    return '<div class="display-message">' . custom_message() . '</div>';
} 
// Register shortcode
add_shortcode( 'display_message', 'display_my_message' );

SHORTCODE USAGE

In an existing page:

[display_message]

Or in PHP:

echo do_shortcode("[display_message]");


Related: WooCommerce shortcode to ajaxify message that display: "Buy X more products to get a discount"

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda