Home > Blockchain >  Adding ACF to Woocommerce ul.products on Shop Page
Adding ACF to Woocommerce ul.products on Shop Page

Time:12-14

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 >
    <?php $location = get_field_object('address'); ?>
    <?php if( ! empty( $location ) ) { ?>
    <div >
    <div ><i >&#xe081;</i><?php echo $location['value'];?></div>
    </div>
    <?php } ?>
    
    <?php $bed = get_field_object('bedroom'); ?>
    <?php if( ! empty( $bed ) ) { ?>
    <div >
    <div ><i ></i><?php echo $bed['value'];?></div>
    <span>Bed</span>
    </div>
    <?php } ?>
    
    <?php $bath = get_field_object('bathroom'); ?>
    <?php if( ! empty( $bath ) ) { ?>
    <div >
    <div ><i ></i><?php echo $bath['value'];?></div>
    <span>Bath</span>
    </div>
    <?php } ?>
</div>

    <?php 
} ?>

CodePudding user response:

I revised your code. Try the below code.

function woo_products_property() {  
    global $post;
    ?>
    <div >
        <?php 
        $location = get_field( 'address', $post->ID ); 
        if( ! empty( $location ) ) { ?>
            <div >
                <div ><i >&#xe081;</i><?php echo $location['value'];?></div>
            </div>
        <?php } 

        $bed = get_field( 'bedroom', $post->ID ); 
        if( ! empty( $bed ) ) { ?>
            <div >
                <div ><i ></i><?php echo $bed['value'];?></div>
                <span>Bed</span>
            </div>
        <?php } 

        $bath = get_field( 'bathroom', $post->ID); 
        if( ! empty( $bath ) ) { ?>
            <div >
                <div ><i ></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

  • Related