2017-05-18 16 views
0

私は、ポストコンタクトフォームのボタンをユーザがクリックすると、プログラムでウオコマース商品を作成するカスタム機能を開発中です。以下は私が製品を作成するために使用しているコードですが、機能コード内で製品IDを取得できないため、この製品をコード経由でカートに追加することに固執しています。商品IDを取得してカートに追加する方法を教えてください。商品名でwoocommerceプロダクト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'); 
+0

$ post_id = wp_insert_post($ post、$ wp_error); // <== ID – metalbox

+0

@metalboxご返信ありがとうございます。 $ post_idは私が探している商品IDであり、商品をカートに追加するために使用できることを意味しますか? –

答えて

0

そうです。下のコード行には、最後に追加された投稿のIDが含まれています。

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

それは最後に働いた。そんなにありがとう、あなたは私に多くの時間を救った。投稿されたおすすめ画像を商品のサムネイルに追加することができますか –

関連する問題