Search code examples
wordpresswoocommerceadvanced-custom-fieldshook-woocommerceacfpro

ACF - Display a photo for selecting a checkbox in Advanced Custom Fields


To sell my products, I need to display file types on the wc single product page. for example : html , css , js , svg

--- I wrote a code for this and it works fine. But I feel it is not standard.

function acf_product_icon_type(){
    $prodoct_icon = get_field_object('product_type_icon');
    $icons = $prodoct_icon['value'];
    if( $icons ): ?>
<ul class="product-platform-selector">
    <?php foreach( $icons as $icon ): ?>
    <li><?php 
switch ($prodoct_icon['choices'][ $icon ]) {
    case "Illustrator":
        echo '<img src="/plat-ai.svg">';break;
    case "Photoshop":
        echo '<img src="/plat-ps.svg">';break;
    case "Xd":
        echo '<img src="/plat-xd.svg">';break;
    case "Lightroom":
        echo '<img src="/plat-lroom.svg">';break; 
}
    ?>
    </li>
    <?php endforeach; ?>
</ul>
<?php endif; 
}
add_shortcode('cicon', 'acf_product_icon_type');

Do you have a suggestion for a better way?


Solution

  • function acf_product_icon_type(){
        $prodoct_icon = get_field_object('product_type_icon');
        $icons = $prodoct_icon['value'];
        if ($icons):
            $icons_img = array(
                'Illustrator' => 'ai',
                'Photoshop' => 'ps',
                'Xd' => 'xd',
                'Lightroom' => 'lroom'
                    )
            ?>
            <ul class="product-platform-selector">
                <?php
                foreach ($icons as $icon):
                    $icon_img_type = $prodoct_icon['choices'][$icon];
                    ?>
                    <li> <?php echo "<img src='/plat-" . $icons_img[$icon_img_type] . ".svg'>"; ?> </li>
            <?php endforeach; ?>
            </ul>
        <?php
        endif;
    }
    
    add_shortcode('cicon', 'acf_product_icon_type');