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

115
Visualizações
Woocommerce function that triggers jquery code

Iam trying to trigger a popup by simulating a button click with a simple JS function when a product from a specific category is added to the woocommerce cart. I have seen some do it in simmilair functions, but I cant figure this one to work.

I have composed the following function that runs if the category is in cart and if the page is the checkout page:

add_action( 'woocommerce_before_checkout_form', 'check_category_in_cart' );
     
    function check_category_in_cart() {
   $cat_in_cart = false;

   foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

      if ( has_term( 'test', 'product_cat', $cart_item['product_id'] ) ) {
         $cat_in_cart = true;
         break;
      }
   }

   if ( $cat_in_cart ) {
 
<script>
    jQuery(document).ready(function($) { 
        $('#pop-up').trigger('click');
    });
</script>
   }
 
}

This function makes my website not function. Anyone has an idea how I can get this to work?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

use setTimeout(). try the below code.

add_action( 'woocommerce_before_checkout_form', 'check_category_in_cart' );
function check_category_in_cart() {

    $cat_in_cart = false;

    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

        if ( has_term( 'test', 'product_cat', $cart_item['product_id'] ) ) {
            $cat_in_cart = true;
            break;
        }
    }

    if ( $cat_in_cart ) { ?>
 
    <script>
        jQuery(document).ready(function($) { 
            setTimeout(function(){
                $('#pop-up').trigger('click');
            }, 10);
        });
    </script>
    <?php }
 
}

 jQuery(document).ready(function($) { 
                
    setTimeout(function(){ 
        $('#pop-up').trigger('click');
    }, 10);

    jQuery(document).on('click','#pop-up',function(){
        alert('trigger');
    });

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="pop-up">popup</button>

about 4 years ago · Juan Pablo Isaza Relatório

0

PHP waits for a php valid code in <?php ?> tags, but you are placing JavaScript, so you have to close the PHP tag before <script> and open a new one after the </script>. Otherwise your jQuery code is fine.

   if ( $cat_in_cart ) {
?>
 
<script>
    jQuery(function() { 
        $('#pop-up').trigger('click');
    });
</script>

<?php
   }
about 4 years ago · Juan Pablo Isaza Relatório

0

Reason need to check.

  1. Maybe Because your id (pop-up) is not placed in your HTML.
  2. Maybe your jQuery is not loaded.
  3. Check in the console what kind of error you found.
  4. your PHP function should properly closed.
  if ( $cat_in_cart ) {
 ?>
<script>
    jQuery(document).ready(function($) { 
        $('#pop-up').trigger('click');
    });
</script>
<?php
   }

if all is good then you can try.

 jQuery(document).ready(function($) { 
var timeInterVal = setInterval(CheckId, 1000);
CheckId function(){
var id  = $("#pop-up");
if(id.length!=0){
 $('#pop-up').trigger('click');
 clearInterval(timeInterVal);
}
}
});

This will work if your Id not found once your id placed then this will stop the timer and trigger pop-up. Thanks.

about 4 years ago · Juan Pablo Isaza 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