'Generate WooCommerce coupon when comment review is approved

I'm trying to generate a coupon code for customer once his review comment in woocommerce product is approved. I have the whole code ready but once comment is approved nothing happen.

My code only works with comment_post hook but not with the comment_unapproved_to_approved hook. Any advice?

function action_comment_post_coupon(  $comment, $comment_ID, $comment_approved, $commentdata ) {  
if ( isset ( $commentdata['comment_author_email'] ) ) {
        // Get author email
        $author_email = $commentdata['comment_author_email'];
        
if ( is_email( $author_email ) ) {
            $value = random_int(100000, 999999);
            $customer = $comment->comment_author_email;
            $coupon_code = $value; // Code
            $amount = '20'; // Amount
            $discount_type = 'percent'; // Type: fixed_cart, percent, fixed_product, percent_product

            $coupon = array(
                'post_title' => $coupon_code,
                'post_content' => '',
                'post_status' => 'draft',
                'post_author' => 1,
                'post_type'     => 'shop_coupon'
            );

            $new_coupon_id = wp_insert_post( $coupon );

            // Add meta
            update_post_meta( $new_coupon_id, 'discount_type', $discount_type );
            update_post_meta( $new_coupon_id, 'coupon_amount', $amount );
            update_post_meta( $new_coupon_id, 'individual_use', 'yes' );
            update_post_meta( $new_coupon_id, 'customer_email', $author_email );
            update_post_meta( $new_coupon_id, 'product_ids',$product_ids );
            update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
            update_post_meta( $new_coupon_id, 'usage_limit', '1' );
            update_post_meta( $new_coupon_id, 'expiry_date', strtotime("+14 days") );
            update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
            update_post_meta( $new_coupon_id, 'free_shipping', 'no' );
            unset($product_ids);
    
        }
}
}   
add_action( 'comment_unapproved_to_approved', 'action_comment_post_coupon', 10, 4 );


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source