2016-08-27 17 views
2

我正在使用Ajax search pro插件與WooCommerce進行高級產品搜索。Ajax搜索專業高級選項 - 結合自定義字段與術語

Ajax search pro插件設置中,有一個選項卡「高級選項」,您可以在其中自定義帖子標題和帖子描述。
例如,如果你搜索鍵入Aven...那麼結果將顯示Avengers ...

在高級選項卡,你可以自定義結果表明蒙山一些自定義字段和得到的東西,如:Avengers 10$結合'post_title''_price'。

我的問題是,我不能將自定義字段與自定義分類結合起來。例如結合'post_title'自定義字段與release_year定製分類是不可能的,有這樣的組合復仇者2012

我想自定義字段之間用一些特殊的組合:

  • 自定義分類術語release_yearin this answer

  • 自定義分類類型(自定義類別類型)tvshow_cat(類似'product_cat'是)
    我想顯示從tvshow_cat分類的前3個相關產品術語(昏迷分離)的字符串。

我該如何做到這一點?

感謝

+0

我編輯了我的問題 –

回答

3

可以構建將複製相關產品元數據的自定義字段格式分類術語的功能。下面是註釋的代碼:

1)子功能(由2個主要功能使用):

// Processing 'release_year' formatting in a string 

function process_release_year($post_id){ 

    $release_years_str = get_the_term_list($post_id, 'release-year', '', ','); 
    $release_years_arr = explode(',', $release_years_str); 
    $count = sizeof($release_years_arr); 
    $first_year = $release_years_arr[ 0 ]; 
    if ($count > 1) { 
     $last_year = $release_years_arr[ $count - 1 ]; 
     $releaseyear_as_text = ' (' . $first_year . ' - ' . $last_year . ')'; 
    } 
    elseif ($count == 1) $releaseyear_as_text = ' ' . $first_year; 
    else $releaseyear_as_text = ''; 

    return $releaseyear_as_text; 
} 


// Processing 'tvshow_cat' formatting in a string (3 coma separated terms in a string) 

function process_tvshow_cat($post_id){ 
    $description_terms = get_the_terms($post_id, 'tvshow_cat'); 
    $count = 0; $description_string = ''; 
    foreach ($description_terms as $description_term) { 
     $count++; 
     if($count < 4){ 
      $description_string .= $description_term; 
      if($count < 3) $description_string .= ', '; 
     } 
    } 
    return $description_string; 
} 


// The two custom fields creation mechanism 

function custom_fields_creation($post_id){ 

    // The release year 
    $test_cf1 = get_post_meta($post_id, 'release_year', true); 
    if(empty($test_cf1)) { 
     // if doesn't exist we create it 
     $release_year = process_release_year($post_id); 
     if(!empty($release_year)) 
      update_post_meta($post_id, 'release_year', $release_year); 
    } 

    // The TV show cat 
    $test_cf2 = get_post_meta($post_id, 'mov_description', true); 
    if(empty($test_cf2)) { 
     // if doesn't exist we create it 
     $description_mov = process_release_year($post_id); 
     if(!empty($description_mov)) 
      update_post_meta($post_id, 'mov_description', $description_mov); 
    } 
} 

這裏要被一次使用的功能(使一個數據庫備份之前)。此功能將爲所有現有產品創建這2個特殊的自定義字段。

// 1. FOR ALL EXISTING PRODUCTS ==> ==> ==> USE IT ONE TIME ONLY! 
add_action('woocommerce_init', 'product_custom_fields_bulk_action'); // To stop it, just comment this line 
function product_custom_fields_bulk_action(){ 

    // Get all published products 
    $products = get_posts(array(
     'post_type' => 'product', 
     'post_status' => 'publish', 
     'numberposts' => -1 
    )); 

    // Iterating each product 
    foreach($products as $product) 
     custom_fields_creation($product->id); 
} 

下面的函數,將創建每次新產品發佈時間自定義字段

// 2. FOR "NEW CREATED" AND PUBLISHED PRODUCT 
add_action('transition_post_status', 'product_custom_fields_action', 10, 3); 
function product_custom_fields_action($new_status, $old_status, $post) { 
    $post_id = $post->ID; 
    if($old_status != 'publish' && $new_status == 'publish' && !empty($post_id) && in_array($post->post_type, array('product'))) 
     custom_fields_creation($post->ID); 
} 

此代碼的推移function.php文件在您的活動子主題或主題...

可以複製你想要的自定義字段...

此代碼已經過測試並可用。