0

I have created custom meta fields for my WooCommerce products. However, the plugin I am using (Advanced Woo Search) is written in JavaScript and I need to access PHP variables. I have used wp_localize_script() to do this. However, in the front-end, my data is not being shown.

Here is my PHP code:

    function js_enqueue_scripts() {
    global $post;
    $text2 = get_post_meta( $post->ID, '_load_speed_field', true );
    $text3 = get_post_meta( $post->ID, '_tyre_brand_field', true );
    $text4 = get_post_meta( $post->ID, '_brand_model_field', true );
    $text5 = get_post_meta( $post->ID, '_run_flat_field', true );
    //Put your variables inside a array
    $arrayname = array(
        'text1' => $text1,
        'text2' => $text2
    );

    //Register Script
    wp_register_script( "advanced-woo-search-pro", plugin_dir_url( __FILE__ ) . "/assets/js/common.js" );

    //Load the Script on Front-End
    wp_enqueue_script( "advanced-woo-search-pro" );

    //Localize script and pass the Data
    wp_localize_script( "advanced-woo-search-pro", "prefix", $arrayname );
}

//Works function on wp_enqueue_scripts Hook
add_action( "wp_enqueue_scripts", "js_enqueue_scripts" );

And here is my JavaScript to access the data from the plugin:

html += result.title + prefix.text1 + prefix.text2;

I have found this filter in the plugin documentation.

enter image description here

Can someone assist me in concatenating my custom fields to the title search result?

Thanks.

4
  • You mentioned using wp_localize_script(), but it's not in any of the code you've shared. Could you share the code showing your use of wp_localize_script? Jul 3, 2019 at 9:54
  • I've updated it now.
    – NDT-AM
    Jul 3, 2019 at 9:58
  • Have you checked that you are getting proper post ID for which you set the ACF field while getting post meta?
    – Bhupen
    Jul 3, 2019 at 13:29
  • Yes I have checked.
    – NDT-AM
    Jul 3, 2019 at 13:33

1 Answer 1

0

Your script should looks like this.

function js_enqueue_scripts() {
    global $post;
    $text2 = get_post_meta( $post->ID, '_load_speed_field', true );
    $text3 = get_post_meta( $post->ID, '_tyre_brand_field', true );
    $text4 = get_post_meta( $post->ID, '_brand_model_field', true );
    $text5 = get_post_meta( $post->ID, '_run_flat_field', true );
    //Put your variables inside a array
    $arrayname = array(
        'text3' => $text3,
        'text2' => $text2
    );
    //Load the Script on Front-End
    wp_enqueue_script( 'aws-script' );

    //Localize script and pass the Data
    wp_localize_script( 'aws-script', "prefix", $arrayname );
}
//Works function on wp_enqueue_scripts Hook
add_action( "wp_enqueue_scripts", "js_enqueue_scripts" );

Common.js is already enqueued by the plugin, you don't have to re-register it again and enqueue.

After adding this code to your activated theme's functions.php file you can access variable in any javascript file like below.

html += result.title + prefix.text3 + prefix.text2;

Please check this solution and let me know if it works.

1
  • I have tried this but it still does not work
    – NDT-AM
    Jul 3, 2019 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.