Indexing woocommerce custom fields

  • Piteus
    Participant
    1 year, 7 months ago #26227

    Hi, I’ve added a custom field in product but WPSOLR doesn’t index it, is it possible? I’m using this code to add the field:

    function create_custom_field() {
        $args = array(
            'id' => 'custom_field',
            'label' => __( 'aaa', 'cfwc' ),
            'class' => 'data-custom-field',
            'desc_tip' => true,
            'description' => __( 'custom_field', 'ctwc' ),
        );
        woocommerce_wp_text_input( $args );
    }
    add_action( 'woocommerce_product_options_general_product_data', 'create_custom_field' );
    
    function save_custom_field( $post_id ) {
        $product = wc_get_product( $post_id );
        $title = isset( $_POST['custom_field'] ) ? $_POST['custom_field'] : '';
        $product->update_meta_data( 'custom_field', sanitize_text_field( $title ) );
        $product->save();
    }
    add_action( 'woocommerce_process_product_meta', 'save_custom_field' );
    
    function display_custom_field() {
        global $post;
    // Check for the custom field value
        $product = wc_get_product( $post->ID );
        $title = $product->get_meta( 'custom_field' );
        if( $title ) {
            echo '<div>'.get_post_meta($post->ID, 'custom_field', true).'</div>';
        }
    }
    add_action( 'woocommerce_product_meta_end', 'display_custom_field' );
    Piteus
    Participant
    1 year, 7 months ago #26228

    Problem solved

    wpsolr
    Keymaster
    1 year, 7 months ago #26229

    As you probably noticed, you just have to select your product custom field in tab 2.2 to get it indexed.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.