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

216
Visualizações
WooCommerce triggering an action and creating a log file when product is published

In WooCommerce, I’m trying to trigger an action when a product is published, but it doesn’t work, here is my code:

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 ); 
}

Here I’m trying to create a file and put some text in it. But as I said, the file isn’t created.

I’m using wordpress 5.8.1 and Woocommerce 5.8.0. What is the problem and how to solve it? Your help would be greatly appreciated.

Thanks in advance.

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

0

The bug is not in the hook itself! It's in the way you've defined the path to the log file.

There are several ways to do this.

The following code is my personal preference because I think it's more flexible and more readable:

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);
    }
}

Note:

  • I named the file "zzz.txt" just to give you an example! Feel free to change its name!
  • $your_custom_file points to the root directory of your theme. Feel free to change it if you would want to save your file in a sub-directory.
  • If your file already exists, then I've put a , to separate the logs. Again feel free to change it!

Another way using file_put_contents function.

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);
    
}

Both solutions have been tested on wordpress 5.8.1 and Woocommerce 5.8 and work fine!

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