• I have an ACF field that is working fine for adding info with each store. I can also pull the field out into the template with:

    add_filter( 'wpsl_frontend_meta_fields', 'custom_frontend_meta_fields' );
    
    function custom_frontend_meta_fields( $store_fields ) {
        $store_fields['products'] = array(
            'name' => 'products_at_this_store',
            'type' => 'text'
        );
        return $store_fields;
    }

    However I need to manipulate the data with PHP before sending it to the JSON Response as it is a serialised field: {i:0;s:2:”95″;i:1;s:2:”97″;i:2;s:2:”96″};

    How can I do that?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter John

    (@jhob)

    I have managed to do this now, but only by hacking a core plugin file.

    I added an option to the switch($meta_type) {} in class-frontend.php to handle the ACF products list:

     case 'products':
    $post_objects = get_field('products', $store->ID);
    $products     = '<h4>Products stocked</h4>';
    if ( $post_objects ) {
      $products.='<ul>';
      foreach ( $post_objects as $post_object ) {
        $products .= '<li>'.get_the_title( $post_object->ID ) . '</li>';
      }
      $products.='</ul>';
    }
    $meta_data =  stripslashes( $products ) ;
    break;

    Obviously this isn’t ideal. Is there a better way of doing this?

    • This reply was modified 8 years, 6 months ago by John.
    Plugin Author Tijmen Smit

    (@tijmensmit)

    If you want to include custom data in each store, then you can do so with this filter.

    
    add_filter( 'wpsl_store_meta', 'custom_store_meta', 10, 2 );
    
    function custom_store_meta( $store_meta, $store_id ) {
    
        $your_custom_data = your_function();
        
        $store_meta['extra_data'] = $your_custom_data;
        
        return $store_meta;
    }
    

    Then include the custom key in the search results template with this filter.

    
    $listing_template .= "\t\t\t" . '<% if ( extra_data ) { %>' . "\r\n";
    $listing_template .= "\t\t\t" . '<p><%= extra_data  %></p>' . "\r\n";
    $listing_template .= "\t\t\t" . '<% } %>' . "\r\n";
    
    • This reply was modified 8 years, 6 months ago by Tijmen Smit.
Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Manipulating data from ACF field before adding to JSON response’ is closed to new replies.