Support

Account

Home Forums Front-end Issues Show ACF field in WooCommerce Archive Listing

Solved

Show ACF field in WooCommerce Archive Listing

  • I’d like to show an ACF field in the Product Archive page. Each product has an ACF field for an extra descriptor. Is there a function I can add to functions.php to make it display under the title of each product listed?

  • WooCommerce has _a literal ton_ of hooks to use. If you open up the plugin folder ( plugins/woocommerce/templates ) you can open up those files and see just how many you can hook into.

    The one you’re looking for is in the content-single-product.php file:

    /**
     * Display value after single product titles
     *
     * @return void
     */
    function prefix_after_title() {
    	echo get_field( 'after_title' );
    }
    add_action( 'woocommerce_single_product_summary', 'prefix_after_title', 6 );

    We need the priority of 6 because the file linked above shows that the title is prioritized at 5. If we wanted to display this before the title we would need to change the priority to something before 5.

  • Thanks very much for your help. Your explanation helped me to understand how hooks work.

    It’s actually on the archive page I want to add the ACF so in content-product.php using ‘woocommerce_after_shop_loop_item_title’ should have worked, but my theme (Astra) also seems to be influencing that area, so in the end I hid the title using the theme customiser and then in functions.php

    /**
     * ADD THE ACF DESCRIPTOR BY REMOVING THE FUNCTION AND REPLACING IT WITH OUR OWN - YOU MUST ALSO HIDE THE TITLE IN ASTRA CUSTOMISER
     */
    function sly_switch_loop_title(){
        remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10 );
        add_action( 'woocommerce_after_shop_loop_item_title', 'sly_template_loop_product_title', 15 );
    }
    add_action( 'woocommerce_before_shop_loop_item', 'sly_switch_loop_title' );
    
    function sly_template_loop_product_title() {
        echo '<h4 class="sly-custom-shop-list-title">' . get_the_title() . '</h4>';
    	echo get_field( 'descriptor' );
    }
  • Hi i want to do something similar. I have a acf input field called “contado_en_local” and i want to replace the woocommerce regular price with it in the archive product page.
    The problem is that the acf field appears, but above the title and i want it below, and also it didn’t removed the woocommerce default regular price.
    Also i don’t how to style it. It is donde in the child-theme’s style.css?
    I made a custom plugin called WOOCOMMERCE ALE BIANCHI and insert this code in wp-content/plugins/woocommerce-ale-bianchi-audioarte.

    
     <?php 
     /* 
     Plugin Name: WOOCOMMERCE ALE BIANCHI
     Plugin URI: https://rampcrosario.com 
     Description: Funciones personalizadas Woocommerce para AudioArte 
     Version: 1.0.0 
     Author: ALE BIANCHI 
     Author URI: https://rampcrosario.com 
     License: GPL 2+ 
     License URI: https://rampcrosario.com 
     */
     
     
     //* Mostrar precio CONTADO creado con ACF en vez de DEBITO colocado en precio regular Woocommerce
    add_action( 'after_setup_theme', 'ale_custom_shop_woocommmerce' );
    
    function ale_custom_shop_woocommmerce () {
    
     remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
     
     function ale_acf_precio() {
        if(get_field('contado_en_local'))
        
    	{
    		echo '<p>' . get_field('contado_en_local') . '</p>';
    	}
    	
    	}
    	add_filter( 'woocommerce_after_shop_loop_item_title', 'ale_acf_precio', 15 );
    
    }
     
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Show ACF field in WooCommerce Archive Listing’ is closed to new replies.