【问题标题】:Ajax search pro advanced option - Combining custom fields with termsAjax search pro 高级选项 - 将自定义字段与术语组合
【发布时间】:2017-01-04 00:13:54
【问题描述】:

我正在将 Ajax search pro 插件与 WooCommerce 一起用于高级产品搜索。

Ajax search pro 插件设置中,有一个“高级选项”选项卡,您可以在其中自定义帖子标题和帖子描述。
例如,如果您输入 Aven... 进行搜索,那么结果将显示 Avengers ...

在高级选项卡中,您可以使用一些自定义字段自定义结果显示并获得类似:Avengers 10$ 结合 'post_title''_price强>'。

我的问题是我无法将自定义字段与自定义分类相结合。例如,将 'post_title' 自定义字段与 release_year 自定义分类组合是不可能的,这样的组合 Avengers 2012

我想在自定义字段之间使用一些特殊组合:

  • 自定义分类术语release_year,例如in this answer

  • 自定义分类类型(自定义类别类型) tvshow_cat (类似于'product_cat'是)。
    我想显示来自 tvshow_cat 分类的前 3 个相关产品术语(逗号分隔)的字符串。

我怎样才能做到这一点?

谢谢

【问题讨论】:

  • 我编辑了我的问题

标签: php wordpress search woocommerce custom-taxonomy


【解决方案1】:

您可以构建一个函数,在产品元数据自定义字段中复制相关的格式化分类术语。以下是注释代码:

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 文件中…

您可以克隆任意数量的自定义字段...

此代码已经过测试并且可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-28
    • 1970-01-01
    • 1970-01-01
    • 2015-09-27
    • 2015-11-04
    • 2017-12-16
    • 2012-01-17
    相关资源
    最近更新 更多