• Resolved Anonymous User 6087458

    (@anonymized-6087458)


    I have created a custom field for my shop using the standard WC hooks however I don’t know how to modify the search query so that searches also look in this custom field.

    The working code to define and save my custom field is as follows…

    // WC custom fields : define field
    function custom_wc_fields_aj() {
    $args = array(
    ‘label’ => __( ‘Product Variant Codes’, ‘woocommerce’ ),
    ‘placeholder’ => __( ‘Enter variant codes here (comma separated)’, ‘woocommerce’ ),
    ‘id’ => ‘aj_variant_codes’,
    ‘desc_tip’ => true,
    ‘description’ => __( ‘These extra variant codes are used for searches.’, ‘woocommerce’ ),
    );
    woocommerce_wp_text_input( $args );
    }
    add_action( ‘woocommerce_product_options_sku’, ‘custom_wc_fields_aj’, 20 );

    // save custom WC fields
    function save_wc_fields_aj( $post_id ) {
    // grab the custom SKU from $_POST
    $custom_sku = isset( $_POST[ ‘aj_variant_codes’ ] ) ? sanitize_text_field( $_POST[ ‘aj_variant_codes’ ] ) : ”;
    $product = wc_get_product( $post_id );
    $product->update_meta_data( ‘aj_variant_codes’, $custom_sku );
    $product->save();
    }
    add_action( ‘woocommerce_process_product_meta’, ‘save_wc_fields_aj’, 20);

    I’ve searched and tried a huge number of sample code items on modifying the WP query if its a search but none work. Does anyone have reference code that definitely works on current versions of WP ?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Aashik P – a11n

    (@aashik)

    Hi there!

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the following places for more development-oriented questions:

    1. WooCommerce Slack Community: https://woocommerce.com/community-slack/
    2. Advanced WooCommerce group on Facebook: https://www.facebook.com/groups/advanced.woocommerce/

    On that note, I found some article for WordPress search which may help you get started: https://adambalee.com/search-wordpress-by-custom-fields-without-a-plugin/

    Thread Starter Anonymous User 6087458

    (@anonymized-6087458)

    @aashik I know there are lots of ways of modifying the search query through hooks but at its heart, this is a really simple question and all I need is a really simple example code answer.

    Hoping someone can help ?

    Plugin Support Aashik P – a11n

    (@aashik)

    Hello @arkid77

    I know there are lots of ways of modifying the search query through hooks but at its heart, this is a really simple question and all I need is a really simple example code answer.

    Hoping someone can help ?

    I’m not aware of the actual complexity of the coding involved here as I’m not a developer myself.

    What’s fairly complex to me maybe really simple for a developer – which is why the topic is still open for other community members to chime in and help out if it is within their capacity ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom field works but not included in search results’ is closed to new replies.