Support

Account

Home Forums Add-ons Repeater Field Get Options repeater meta_value, not dynamically populated select's meta_value

Solving

Get Options repeater meta_value, not dynamically populated select's meta_value

  • First off, forgive me if I’m using some incorrect language, I’m more of a designer than a developer…

    I have a custom post type of ‘trails’.

    Each trail is a certain duration, from 2 Days up to 6 Days. Each duration needs to be linked to a price. For example, all 2 Day trails are £200, all 3 Day trails are £280 etc.

    I have created an Options page with a repeater field where I can create trail durations and add a price for each.

    I have then used the following code to pull these rows in to a select field on each trail in the WP admin:

    function acf_load_duration_and_pricing_field_choices( $field ) {
        
        // reset choices
        $field['choices'] = array();
    
        // if has rows
        if( have_rows('options_trail_duration_and_price', 'option') ) {
            
            // while has rows
            while( have_rows('options_trail_duration_and_price', 'option') ) {
                
                // instantiate row
                the_row();
                
                
                // vars
                $duration = get_sub_field('trail_duration');
                $price = get_sub_field('trail_price');
    
                
                // append to choices
                $field['choices'][ $price ] = $duration;
                
            }
            
        }
    
        // return the field
        return $field;
        
    }
    
    add_filter('acf/load_field/name=duration_and_pricing', 'acf_load_duration_and_pricing_field_choices');

    options_trail_duration_and_price is the name of the repeater on the options page.
    trail_duration is a repeater sub field.
    trail_price is the other repeater sub field.
    duration_and_pricing is the name of the select field on all trails posts.

    The prices change regularly on these trails. What I’m trying to do is create two shortcodes: one that outputs that trail’s duration and one that outputs that trail’s price. Essentially the first needs to output the label and the latter the value.

    I’ve found ways to output the label and value from duration_and_pricing, but when I change that trail_price in the options page it doesn’t update on the trail post itself, unless I manually Update the trail post (I have confirmed this by looking at the postmeta table in PHPMyAdmin where it shows the old price in meta_value). This led me to think I probably need to grab the trail price from the options page repeater itself rather than the outdated price from the duration_and_pricing select field, however every code snippet I tried just output the trail_price from every options_trail_duration_and_price row, and not the one selected for that trail.

    My best shot was the following shortcode:

    function trailprice_shortcode() {
    if( have_rows('options_trail_duration_and_price', 'option') ): ?>
    
        <?php while( have_rows('options_trail_duration_and_price', 'option') ): the_row();
    	echo $trailprice;
    	$trailprice = get_sub_field('trail_price', 'option');
        endwhile;
    endif;
    add_shortcode('trailprice', 'trailprice_shortcode');

    This seemed to work at first. It was outputting the latest trail price as per the options page even after I changed it, but then I realised it’s only outputting the price from the first row, regardless of which duration is selected in the duration_and_pricing select in each trail.

    So I guess my question is: how do I adapt this shortcode code to make sure it gets and outputs the latest trail_price of the row selected in the trail duration_and_pricing select field for that trail?

    I’ve been chipping away at this for over a week and I’m not even sure if it’s possible, so any help would be hugely appreciated. I hope this is clear it’s very complicated to explain! Thanks in advance!

  • Another way to do this might be to hook in to the Update button in the Options page and have it update the meta_value of duration_and_pricing across all trails posts when the prices are updated in the options, but that seems like overkill and I’m still not sure how to do it.

  • I have more or less found a solution… Somehow!

    I switched the dynamic population of the select box around so that it’s using the trail length as the value, as this will never change but the price will, and I need to match the value of the select to one of the repeater sub-fields.

    Here’s how I’m populating the select now:

    function acf_load_duration_and_pricing_field_choices( $field ) {
        
        // reset choices
        $field['choices'] = array();
    
        // if has rows
        if( have_rows('options_trail_duration_and_price', 'option') ) {
            
            // while has rows
            while( have_rows('options_trail_duration_and_price', 'option') ) {
                
                // instantiate row
                the_row();
                
                // vars
                $duration = get_sub_field('trail_duration');
                $price = get_sub_field('trail_price');
    
                // append to choices - this is the bit I've changed
                $field['choices'][ $duration ] = $duration . ' - ' . '£' . $price . 'pp';
                
            }
            
        }
    
        // return the field
        return $field;
        
    }
    
    add_filter('acf/load_field/name=duration_and_pricing', 'acf_load_duration_and_pricing_field_choices');

    And now I’m pulling in the trail price through this shortcode:

    if( have_rows('options_trail_duration_and_price', 'option') ): ?>
    
        <?php while( have_rows('options_trail_duration_and_price', 'option') ): the_row();
    	    
    		$optionduration = get_sub_field('trail_duration', 'option');
    		$optionprice = get_sub_field('trail_price', 'option');
    		$duration = get_field('duration_and_pricing')['value'];
    		if( $optionduration == $duration )
    		{
    			echo '<span class="YES!">£' . $optionprice . 'pp</span>';
    		}
    
        endwhile;
    
    endif;

    Matching the select’s value (e.g. ‘2 Days’) with the repeater’s trail_duration value (‘2 Days’), then outputting the trail_price of that associated row, rather than the trail_price stored in the select’s meta_value.

    For some reason in some cases, it’s outputting the price twice, but at this point I’m just glad it’s working in some way!

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

You must be logged in to reply to this topic.