Support

Account

Home Forums Front-end Issues Output Product or text value from select

Solving

Output Product or text value from select

  • Hi! I need some guidance on how to output the selected values from a select on front.

    I have two options, on is a text field with a populated html value, and the second one is a post_object for products where I want to display the thumb and the permalink.

    So either output the the hmtl value or the products thumb and link.

    so something like:

    <div class="select populated">
    choice 1 or choice 2
    </div>

    This is how its setup in ACF:

    Screenshot

    Return format on select set to both (array). Display output set to Post object.

    'sub_fields' => array( 
    	                   					array(
                            'key' => 'field_5f7efd35f2428',
                            'label' => 'Block one select type',
                            'name' => 'menu_item_type',
                            'type' => 'select',
                            'instructions' => '',
                            'required' => 0,
                            'conditional_logic' => 0,
                            'wrapper' => array(
                                'width' => '',
                                'class' => '',
                                'id' => '',
                            ),
                            'choices' => array(
                                'checkbox' => 'Checkbox',
                                'product' => 'Product',
                            ),
                            'default_value' => array(
                            ),
                            'allow_null' => 0,
                            'multiple' => 0,
                            'ui' => 0,
                            'ajax' => 0,
                            'return_format' => 'value',
                            'placeholder' => '',
                        ),
                        array(
                            'key' => 'field_5f7efde2f2429',
                            'label' => 'Checkbox',
                            'name' => 'checkboxfield',
                            'type' => 'text',
                            'instructions' => '',
                            'required' => 0,
                            'conditional_logic' => array(
                                array(
                                    array(
                                        'field' => 'field_5f7efd35f2428',
                                        'operator' => '==',
                                        'value' => 'checkbox',
                                    ),
                                ),
                            ),
                            'wrapper' => array(
                                'width' => '',
                                'class' => '',
                                'id' => '',
                            ),
                            'return_format' => 'array',
                            'default_value' => '<i class="fas fa-check font-size-60 white width-1em"></i>',
                        ),
                        array(
                            'key' => 'field_5f7f050f2d6e7',
                            'label' => 'Product',
                            'name' => 'pdp_product_one',
                            'type' => 'post_object',
                            'instructions' => '',
                            'required' => 0,
                            'conditional_logic' => array(
                                array(
                                    array(
                                        'field' => 'field_5f7efd35f2428',
                                        'operator' => '==',
                                        'value' => 'product',
                                    ),
                                ),
                            ),
                            'wrapper' => array(
                        'width' => '',
                        'class' => '',
                        'id' => '',
                    ),
                    'post_type' => array(
                        0 => 'product',
                    ),
                    'taxonomy' => array(
                    ),
                    'allow_null' => 1,
                    'multiple' => 0,
                    'return_format' => 'object',
                    'ui' => 1,
    				),        

    Tried a bunch of different ways but no luck, so need to start over. Any help would be appreciated!

  • 
    $choice = get_field('menu_item_type');
    if ($choice == 'checkbox') {
       // do something
    } elseif ($choice == 'product') {
       // do something else
    }
    
  • Thanks John. For the first one it would work, I can just add my html in the if statement.

    But the second one – it has Post object set to products, so there I need to call the selected product. (link and thumb)

  • 
    $choice = get_field('menu_item_type');
    if ($choice == 'checkbox') {
       // do something
    } elseif ($choice == 'product') {
       $product = get_field('pdp_product_one');
       // get an acf field from the product
       $some_value = get_field('some_field', $product->ID);
       // get something else from the product
       // see documentation for what you are using for products
    }
    
  • Thanks again, this doesnt work unfortunately. And I dont want an if or else statement, I want to output the data from the selected select fields which already has information (either text field or a post object).

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

You must be logged in to reply to this topic.