如何使用 woocommerce_trash_{$post_type} 动作挂钩

How to use woocommerce_trash_{$post_type} action hook

当管理员点击产品的垃圾桶时,我正在尝试读取产品价值(附图片)。

经过一番搜索,我发现 woocommerce_trash_{$post_type} 钩子可以用于此目的。

我在 class-wc-product-data-store-cpt.php 行 307 @version 3.0.0

上找到了详细信息

我应用了代码:

add_action('woocommerce_trash_product', 'My_custom_trash_product');
function My_custom_trash_product($product_id) {
    error_log(print_r($product_id, true));
}

但它没有开火。我正在使用 WooCommerce 6.0.0。有什么建议吗?

参见:Trash/delete product hooks don't fire

We don't control the WordPress UI delete and trash functionality, so it bypasses CRUD completely.

If you need to detect these events across CRUD and across WordPress UI, use wp_trash_post and wp_delete_post actions. If we ever have our own UI (not WP UI) we'll be able to consistently fire them everywhere.

所以您也可以使用:

/**
 * Fires before a post is sent to the Trash.
 *
 * @since 3.3.0
 *
 * @param int $post_id Post ID.
 */
function action_wp_trash_post( $post_id ) {
    error_log( print_r( $post_id, true ) );
}
add_action( 'wp_trash_post', 'action_wp_trash_post', 10, 1 );

感谢@7uc1f3r 的回复。它正在工作,我还发现了另一个动作挂钩 trashed_post from here,它在 post 被发送到垃圾箱后触发。

我在这里提交了我的解决方案 -

/**
 * Fires after a post is sent to the Trash.
 *
 * @since 2.9.0
 *
 * @param int $post_id Post ID.
 */
function action_trashed_post( $post_id ) {
    error_log( print_r( $post_id, true ) );
}
add_action( 'trashed_post', 'action_trashed_post', 10, 1 );