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

219
Views
WooCommerce activa una acción y crea un archivo de registro cuando se publica el producto

En WooCommerce, intento activar una acción cuando se publica un producto, pero no funciona, aquí está mi código:

 add_action( 'transition_post_status', 'my_call_back_function', 10, 3 ); function my_call_back_function( $new_status, $old_status, $post ) { if ( 'product' !== $post->post_type || 'publish' !== $new_status || 'publish' === $old_status ) { return; } file_put_contents( 'file.txt', 'Product published', FILE_APPEND ); }

Aquí estoy tratando de crear un archivo y poner algo de texto en él. Pero como dije, el archivo no se crea.

Estoy usando wordpress 5.8.1 y Woocommerce 5.8.0. ¿Cuál es el problema y cómo solucionarlo? Su ayuda sería muy apreciada.

Gracias por adelantado.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

¡El error no está en el gancho en sí! Está en la forma en que definió la ruta al archivo de registro.

Hay varias maneras de hacer esto.

El siguiente código es mi preferencia personal porque creo que es más flexible y más legible:

 add_action('transition_post_status', 'my_call_back_function', 10, 3); function my_call_back_function($new_status, $old_status, $post) { if ( 'product' !== $post->post_type || 'publish' !== $new_status || 'publish' === $old_status ) { return; } $your_custom_file = __DIR__ . '/zzz.txt'; if (!file_exists($your_custom_file)) { $file = fopen($your_custom_file, 'w'); fwrite($file, 'Product published'); fclose($file); } else { $file = fopen($your_custom_file, 'a'); fwrite($file, ','); fwrite($file, 'Product published'); fclose($file); } }

Nota:

  • Llamé al archivo "zzz.txt" solo para darte un ejemplo. ¡Siéntase libre de cambiar su nombre!
  • $your_custom_file apunta al directorio raíz de su tema. Siéntase libre de cambiarlo si desea guardar su archivo en un subdirectorio.
  • Si su archivo ya existe, entonces puse un , para separar los registros. De nuevo, siéntete libre de cambiarlo.

Otra forma de usar la función file_put_contents .

 add_action('transition_post_status', 'my_call_back_function', 10, 3); function my_call_back_function($new_status, $old_status, $post) { if ( 'product' !== $post->post_type || 'publish' !== $new_status || 'publish' === $old_status ) { return; } $your_custom_file = __DIR__ . '/zzz.txt'; file_put_contents($your_custom_file, 'Product published', FILE_APPEND); }

¡Ambas soluciones han sido probadas en wordpress 5.8.1 y Woocommerce 5.8 y funcionan bien!

over 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!