Categories
WordPress

WordPress Auto Generate Post

To create a page for a user when purchase some product:

add_action( 'woocommerce_order_status_completed', 'condless_create_page' );
function condless_create_page( $order_id ) {
        foreach ( wc_get_order( $order_id )->get_items() as $item ) {
                if ( $item->get_product_id() === 49 ) {
                        $post_details = array(
                                'post_title' => 'My New Page',
                                'post_status' => 'publish',
                                'post_author' => wc_get_order( $order_id )->get_user_id(),
                                'post_type' => 'page',
                        );
                        wp_insert_post( $post_details );
                }
        }
}

Leave a Reply

Your email address will not be published. Required fields are marked *