Search code examples
phpwordpresswoocommerceadvanced-custom-fieldscustom-fields

Adding ACF to Woocommerce ul.products on Shop Page


I'm using WooCommerce and Advanced Custom Fields, where ACF group is set up with post type for products. I would like to add a couple of simple text fields to the products box below the product title, and it will display on all products.

I have looked and found the hook for this to be woocommerce_after_shop_loop_item_title

Image attached for visual description.

enter image description here

Like this I'm looking to add Address value ($location), Bedrooms value ($bed) and Bathrooms ($bath).

Please understand I am very new to PHP and still learning. I have tried to make an attempt, however I am unsure how to extract the field data from the product's post.

Any tips in the right direction to learn would really be much appreciated.

Thank you in advance.

add_action( 'woocommerce_after_shop_loop_item_title', 'woo_products_property', 1 );
function woo_products_property() { 
    ?>          

<div class="property_loop_bottom_sec">
    <?php $location = get_field_object('address'); ?>
    <?php if( ! empty( $location ) ) { ?>
    <div class="feature">
    <div class="value"><i class="et-pb-icon map-marker">&#xe081;</i><?php echo $location['value'];?></div>
    </div>
    <?php } ?>
    
    <?php $bed = get_field_object('bedroom'); ?>
    <?php if( ! empty( $bed ) ) { ?>
    <div class="feature">
    <div class="value"><i class="fas fa-bed"></i><?php echo $bed['value'];?></div>
    <span>Bed</span>
    </div>
    <?php } ?>
    
    <?php $bath = get_field_object('bathroom'); ?>
    <?php if( ! empty( $bath ) ) { ?>
    <div class="feature">
    <div class="value"><i class="fas fa-bath"></i><?php echo $bath['value'];?></div>
    <span>Bath</span>
    </div>
    <?php } ?>
</div>

    <?php 
} ?>

Solution

  • I revised your code. Try the below code.

    function woo_products_property() {  
        global $post;
        ?>
        <div class="property_loop_bottom_sec">
            <?php 
            $location = get_field( 'address', $post->ID ); 
            if( ! empty( $location ) ) { ?>
                <div class="feature">
                    <div class="value"><i class="et-pb-icon map-marker">&#xe081;</i><?php echo $location['value'];?></div>
                </div>
            <?php } 
    
            $bed = get_field( 'bedroom', $post->ID ); 
            if( ! empty( $bed ) ) { ?>
                <div class="feature">
                    <div class="value"><i class="fas fa-bed"></i><?php echo $bed['value'];?></div>
                    <span>Bed</span>
                </div>
            <?php } 
    
            $bath = get_field( 'bathroom', $post->ID); 
            if( ! empty( $bath ) ) { ?>
                <div class="feature">
                    <div class="value"><i class="fas fa-bath"></i><?php echo $bath['value'];?></div>
                    <span>Bath</span>
                </div>
            <?php } ?>
        </div>
    
    <?php } 
    add_action( 'woocommerce_after_shop_loop_item_title', 'woo_products_property', 1 );
    

    Tested and works

    enter image description here