I am working on a custom function to create a woocommerce product programmatically when user click a button on the post contact form. The below is the code i am using to create the product, but yet i am stuck to add this product to the cart via code since i can not manage to get the product id within the function code. Need your help to tell me how to get the product id to add it to the cart.

    function contactform7_before_send_mail( $tour_to_product ) {

    $tour_to_product = WPCF7_Submission::get_instance();

    if ( $tour_to_product ) {
    $formData = $tour_to_product->get_posted_data();
    }

        $tourprice =$formData['tour-price'];
        $tourdiscountprice =$formData['tour-discount-price'];
        $tourname =$formData['tour-name'];
        $noadults =$formData['no-adult'];
        $nochild =$formData['no-child'];
        if(!empty($formData['tour-discount-price'])) 
        {
            $finalprice =$formData['tour-discount-price'];
        } else {
            $finalprice =$formData['tour-price']; 
        }

    $post = array(
        'post_author' => $user_id,
        'post_content' => '',
        'post_status' => "publish",
        'post_title' => $tourname,
        'post_parent' => '',
        'post_type' => "product",
    );

    //Create post
    $post_id = wp_insert_post( $post, $wp_error );
    if($post_id){
        global $tour_id;
        $attach_id = get_post_meta($product->parent_id, "_thumbnail_id", true);
        add_post_meta($post_id, '_thumbnail_id', $attach_id);
// i am trying here to get the product id
        $tour_id = get_post_meta($product->id());
    }

    wp_set_object_terms( $post_id, 'Tours', 'product_cat' );
    wp_set_object_terms( $post_id, 'simple', 'product_type');

    update_post_meta( $post_id, '_visibility', 'visible' );
    update_post_meta( $post_id, '_stock_status', 'instock');
    update_post_meta( $post_id, 'total_sales', '0');
    update_post_meta( $post_id, '_downloadable', 'no');
    update_post_meta( $post_id, '_virtual', 'yes');
    update_post_meta( $post_id, '_regular_price', $tourprice );
    update_post_meta( $post_id, '_sale_price', $tourdiscountprice );
    update_post_meta( $post_id, '_purchase_note', "" );
    update_post_meta( $post_id, '_featured', "no" );
    update_post_meta( $post_id, '_sku', "");
    update_post_meta( $post_id, '_product_attributes', array());
    update_post_meta( $post_id, '_sale_price_dates_from', "" );
    update_post_meta( $post_id, '_sale_price_dates_to', "" );
    update_post_meta( $post_id, '_price', $tourprice );
    update_post_meta( $post_id, '_sold_individually', "" );
    update_post_meta( $post_id, '_manage_stock', "no" );
    update_post_meta( $post_id, '_backorders', "no" );
    update_post_meta( $post_id, '_stock', "" );
    // i am trying to output the product id for testing    
    echo "<script type='text/javascript'>alert('$tour_id')</script>";

    }

    remove_all_filters ('wpcf7_before_send_mail');
    add_action( 'wpcf7_before_send_mail', 'contactform7_before_send_mail' );

Read more here: Get woocommerce product ID by product name


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: