javascript - Add JQuery As Code Snippet

admin2025-01-07  6

I would like to add jquery code snippet for Woocommerce checkout page for a specific product in the cart, but I'm confused on how to adapt the code to wordpress. Any ideas? Here is the code:

 add_action( 'woocommerce');
function woocommerce() {
$product_id = 18; // a specific product ID you would like to check
if( WC()->cart->find_product_in_cart( WC()->cart->generate_cart_id( 
$product_id ) ) ) {

  <!-- JQuery Code -->
  <script type="text/javascript">
  var jq = jQuery.noConflict();
  jq(window).load(function(){
  jq('#email').on('input', function() {
  jq( "div.tip" ).show();
  jq( "#h2" ).show();
  });
  jq('div.tip').hide();
  jq( "#h2" ).hide();
  });
  </script>
  <!-- JQuery Code -->

   }
}

I would like to add jquery code snippet for Woocommerce checkout page for a specific product in the cart, but I'm confused on how to adapt the code to wordpress. Any ideas? Here is the code:

 add_action( 'woocommerce');
function woocommerce() {
$product_id = 18; // a specific product ID you would like to check
if( WC()->cart->find_product_in_cart( WC()->cart->generate_cart_id( 
$product_id ) ) ) {

  <!-- JQuery Code -->
  <script type="text/javascript">
  var jq = jQuery.noConflict();
  jq(window).load(function(){
  jq('#email').on('input', function() {
  jq( "div.tip" ).show();
  jq( "#h2" ).show();
  });
  jq('div.tip').hide();
  jq( "#h2" ).hide();
  });
  </script>
  <!-- JQuery Code -->

   }
}
Share Improve this question asked Jul 23, 2019 at 1:47 DorianDorian 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

Create js file and add it to your child theme script folder. Then just add code to functions.php, example if page is checout wp_enqueue that file.

Example: https://sridharkatakam.com/how-to-load-woocommerce-css-and-js-only-on-shop-specific-pages-in-wordpress/

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736261743a765.html

最新回复(0)