admin管理员组

文章数量:1126097

I am trying to run a function every time a new product is added on woocommerce, for so I am using woocommerce_new_product action. For this I add a new action

function uniqueNameFunction($id) {
    wp_die("event worked");
}
add_action('woocommerce_new_product', 'uniqueNameFunction', 10, 1);

I suspect maybe I do not register it correctly in my plugin, so to make sure I do it right I regenerated a plugin using this tool and add it along with other actions already in the template.

In order to test it I just add a new product using admin UI.

This does not fire, and I have no idea why and how to debug it. Would be nice if someone gives me some ideas. Thanks!

I am trying to run a function every time a new product is added on woocommerce, for so I am using woocommerce_new_product action. For this I add a new action

function uniqueNameFunction($id) {
    wp_die("event worked");
}
add_action('woocommerce_new_product', 'uniqueNameFunction', 10, 1);

I suspect maybe I do not register it correctly in my plugin, so to make sure I do it right I regenerated a plugin using this tool and add it along with other actions already in the template.

In order to test it I just add a new product using admin UI.

This does not fire, and I have no idea why and how to debug it. Would be nice if someone gives me some ideas. Thanks!

Share Improve this question edited Oct 1, 2018 at 10:36 Marek asked Oct 1, 2018 at 10:31 MarekMarek 1135 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 3

Try like

function add_this_to_new_products( $new_status, $old_status, $post ) {
    global $post;
    if ( $post->post_type !== 'product' ) return;
    if ( 'publish' !== $new_status or 'publish' === $old_status ) return;
    add_post_meta( $post->ID, 'total_amount', '0', true ); // This is the action to take
}

add_action( 'transition_post_status', 'add_this_to_new_products', 10, 3 );

It doesn't fire because there's a issue with this hook.

As stated by the support team on this -> https://github.com/woocommerce/woocommerce/issues/23610

Looks like it does fire when you puts the product on draft, but most users (like you and me) don't see this way. Should fires every time a product is created.

本文标签: woocommerce offtopicwoocommercenewproduct action doesn39t fire