Php 按产品名称获取woocommerce产品ID

Php 按产品名称获取woocommerce产品ID,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我正在使用一个自定义函数,当用户单击post contact表单上的按钮时,以编程方式创建woocommerce产品。下面是我用来创建产品的代码,但我仍然坚持通过代码将此产品添加到购物车中,因为我无法在功能代码中获取产品id。需要您的帮助来告诉我如何获取产品id以将其添加到购物车 function contactform7_before_send_mail( $tour_to_product ) { $tour_to_product = WPCF7_Submission::ge

我正在使用一个自定义函数,当用户单击post contact表单上的按钮时,以编程方式创建woocommerce产品。下面是我用来创建产品的代码,但我仍然坚持通过代码将此产品添加到购物车中,因为我无法在功能代码中获取产品id。需要您的帮助来告诉我如何获取产品id以将其添加到购物车

    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' );
发送邮件前功能联系人格式7($tour\u to\u产品){
$tour_to_product=WPCF7_Submission::get_instance();
if($tour\u to\u产品){
$formData=$tour_to_product->get_posted_data();
}
$tourprice=$formData['tour-price'];
$tourdiscountprice=$formData['tour-discount-price'];
$tourname=$formData['tour-name'];
$noadults=$formData['no-ADULENT'];
$nochild=$formData['no-child'];
如果(!empty($formData['tour-discount-price']))
{
$finalprice=$formData[‘旅游折扣价’];
}否则{
$finalprice=$formData['tour-price'];
}
$post=数组(
“post\u author'=>$user\u id,
'发布内容'=>'',
“发布状态”=>“发布”,
“post_title”=>$tourname,
“post_parent'=>”,
“发布类型”=>“产品”,
);
//创建帖子
$post\u id=wp\u insert\u post($post$wp\u error);
如果($post_id){
全球$tour_id;
$attach\u id=get\u post\u meta($product->parent\u id,“\u缩略图\u id”,true);
添加发布元($post\u id,''u缩略图\u id',$attach\u id);
//我在这里尝试获取产品id
$tour\u id=get\u post\u meta($product->id());
}
wp_set_object_术语($post_id,'Tours,'product_cat');
wp_set_object_术语($post_id,'simple','product_type');
更新发布元($post\u id、'u可见性、'visible');
更新发布元数据($post\u id、'u stock\u status、'instock');
更新发布元数据($post\u id,'total\u sales','0');
更新发布元数据($post\u id,'.'可下载','否');
更新发布元数据($post\u id、'u virtual、'yes');
更新发布元($post\u id,''u常规价格',$tourprice);
更新发布元($post\u id、$sale\u price、$tourdiscountprice);
更新发布元($post\u id,''u purchase\u note','');
更新发布元($post\u id、'u featured'、'no');
更新发布元数据($post\u id,''u sku','');
更新发布meta($post\u id,'.\u product\u attributes',array());
更新发布元($post\u id,''u sale\u price\u dates\u from','');
更新发布元数据($post\u id,''u sale\u price\u dates\u to','');
更新发布元($post\u id,''u price',$tourprice);
更新发布元($post\u id,''u selled','');
更新发布元($post\u id、'u manage\u stock'、'no');
更新发布元数据($post\u id,''u backorders','no');
更新发布元($post\u id,''u stock','');
//我正在尝试输出产品id以进行测试
回显“警报('$tour_id')”;
}
移除所有过滤器(“发送邮件前的wpcf7”);
添加操作(“发送邮件前的wpcf7”、“发送邮件前的联系人表单7”);

没错。下面的代码行包含最后添加的帖子的id

$post_id = wp_insert_post( $post, $wp_error ); //<== ID

$post\u id=wp\u insert\u post($post,$wp\u error)//$post\u id=wp\u insert\u post($post$wp\u error)//@谢谢你的回复。你的意思是$post_id是我正在搜索的产品id,可以用来将产品添加到购物车中吗?它终于成功了。非常感谢你,你为我节省了很多时间。你能帮我把帖子的特色图片添加到产品缩略图中吗