Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

275
Views
¿Cómo agregar datalayer.push en WordPress functions.php?

Quiero agregar datalayer.push en WordPress functions.php, probé el siguiente código pero arroja un error, Error no detectado: Llamar a una función miembro get_id() en una cadena...

 add_action('wp_head', 'datalayer_push'); function datalayer_push($product) { ?> <script> window.dataLayer = window.dataLayer || []; dataLayer.push({ 'itemId': '<?php echo $product->get_id(); ?>' }); </script> <?php }

¿Cómo implementar correctamente el código datalayer.push en functions.php?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Tiene un error porque no hay una variable $product en wp_head de forma predeterminada. Para que tu código funcione, tienes que reescribirlo así:

 add_action('wp_head', 'datalayer_push'); function datalayer_push($product) { // Do not do anything if the current page is not a single product page if ( ! is_product() ) { return; } global $post; $product_id = $post->ID; $product = wc_get_product( $product_id ); ?> <script> window.dataLayer = window.dataLayer || []; dataLayer.push({ 'itemId': '<?php echo $product->get_id(); ?>' }); </script> <?php }
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!