Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save robertdevore/e57b2f2d6fb8bce46558d672618db7dd to your computer and use it in GitHub Desktop.

Select an option

Save robertdevore/e57b2f2d6fb8bce46558d672618db7dd to your computer and use it in GitHub Desktop.
Do something when a product is published in WooCommerce
<?php
/**
* @snippet Do Something When Product is Published
* @how-to Watch tutorial @ https://businessbloomer.com/?p=19055
* @sourcecode https://businessbloomer.com/?p=543
* @author Rodolfo Melogli
* @testedwith WooCommerce 3.2.6
*/
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 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment