Support

Account

Home Forums General Issues Repeater field and creating shortcode.

Helping

Repeater field and creating shortcode.

  • I am using one Field Group – Products (with fields for price, descript, etc.)
    In the Field Group there is a select field for product types.
    Single
    Combo (to combine a number of single products)
    If Combo is chosen,
    an additional repeater field option is presented (Field Type : post object returning postId)

    I have this working to dynamically prepare a single field on update….

    add_filter('acf/prepare_field/key=field_5a64f5e14375a', function($field) {
      $page = get_the_ID(); {
      $field['value'] = '<div class="stack">[acf field=\'pd_snip\' post_id=\''. $page . '\']<b>[acf field=\'pd_price\' post_id=\''. $page . '\']</b>
    
    [acf field=\'sc_add_to_cart_button\' post_id=\''. $page . '\']
    <span class="hide">[acf field=\'sc_text_link\' post_id=\''. $page . '\']</span>
    </div>';
      return $field;
    }
    });

    What I can’t wrap my head around is how to set the $field[‘value’] to create the shortcode for each of the postIDs in the combo…

    ex.

    $field[‘value’] = ‘<div class=”stack”>[acf field=\'pd_snip\' post_id=\''. $combo1 . '\']acf field=\’pd_snip\’ post_id=\”. $combo2 . ‘\’]<b>[acf field=\'pd_price\' post_id=\''. $page . '\']</b>

    [acf field=\'sc_add_to_cart_button\' post_id=\''. $page . '\']
    <span class=”hide”>[acf field=\'sc_text_link\' post_id=\''. $combo1 . '\']
    [acf field=\'sc_text_link\' post_id=\''. $combo2 . '\']</span>
    </div>’;
    return $field;

    The field is intended to be used by client with shortcode in content ‘[acf field=cart_stack post_id="33"]

    I hope this is clear, but I’m a bit of a noob.

  • Hey ckka,

    So, the first thing you’ll need to do is determine what product type you’re looking at, so in your function, you could do something like this:

    
    $product_type = get_field('product_type'); // Or whatever you've named your select field
    

    Then, if you’ve got a ‘Combo’ product type, you’ll need to loop through each of the chosen post objects:

    
    if ( $product_type == 'Combo' )
    {
        // get the Posts and deal with them here
        $combination = get_field( 'combination' ); // or whatever you've named your repeater
        if ( ! empty( $combination ) )
        {
            foreach ( $combination as $combination_item )
            {
                // each item is returned as an array with a key of 'post':
                $post_id = $combination_item['post'];
            }
        }
    }
    else
    {
        // deal with the single item here
    }
    

    So, to put it all together, it might look something like this:

    
    function prepare_stack( $field )
    {
        $page = get_the_ID();
        $hidden = '';
        $value = '
            <div class="stack">';
    
        $product_type = get_field( 'product_type' );
        if ( $product_type == 'Combo' )
        {
            $combination = get_field( 'combination' );
            if ( ! empty( $combination ) )
            {
                foreach( $combination as $combination_item  )
                {
                    $post_id = $combination_item['post'];
                    $value .= '
                [acf field=\'pd_snip\' post_id=\''. $post_id . '\']';
                    $hidden .= '
                    [acf field=\'sc_text_link\' post_id=\''. $post_id . '\']';
                }
            }
        }
        else
        {
            $value .= '
                [acf field=\'pd_snip\' post_id=\''. $page . '\']';
            $hidden .= '
                    [acf field=\'sc_text_link\' post_id=\''. $page . '\']';
        }
    
        $value .= '
                <b>[acf field=\'pd_price\' post_id=\''. $page . '\']</b>
                [acf field=\'sc_add_to_cart_button\' post_id=\''. $page . '\']';
    
        $value .= '
                <span class="hide">'.$hidden.'
                </span>';
    
        $value .= '
            </div>
        ';
    
        $field['value'] = $value;
        return $field;
    }
    add_filter('acf/prepare_field/key=field_5a64f5e14375a', 'prepare_stack');
    

    You can see I used a variable called $hidden while I was looping through the combination posts so I could add it in later.. this means I don’t have to loop through twice.

    This code is untested, so you should go through it line by line to make sure it does what you need it to do!

    Hope this helps..

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

The topic ‘Repeater field and creating shortcode.’ is closed to new replies.