Support

Account

Home Forums General Issues Show the ACF field in WooCommerce Category inside the Shop page

Solved

Show the ACF field in WooCommerce Category inside the Shop page

  • I’ve been cracking my brain over this being a non-programmer. And have gotten so far as to retrieving either the product ID or category ID for each product in the Shop page.

    But I’m stumped on how to combine them to get my ACf field to show.

    Here’s what I got so far..

    function show_shop_suffix() {
    	
    	// If shop page 
    	if ( is_shop() ) {
    		
    		global $product;
    		// to get product ids
    		//$product_id = $product -> get_id();
    		
    		// to show product arrays
    		//$terms = get_the_terms( $product_id, 'product_cat' );
    		
    		// get the product category ids
    		$product_cat_id = wc_get_product_term_ids( $product -> get_id(), 'product_cat' );
    		
    		// vars to get the ACF field and product ID
    		$sugo_loop_item_suffix = get_field( 'product_archive_suffix', 'product_cat' . '_' . $product_cat_id );
    	}
    	
    	echo ($sugo_loop_item_suffix);
    	
    	var_dump ($product_cat_id);
    }
    add_action( 'woocommerce_after_shop_loop_item', 'show_shop_suffix', 5 );

    Entering this code via Code Snippets, instead of a child theme and modifying the WooCommerce template.

  • What is this outputting

    
    var_dump ($product_cat_id);
    
  • Hey John, thank you for helping me out with this.

    Buddy of mine who’s more experienced in development helped me out already. And here was the final code being used now:

    function sd_loop_product_cat_note() {
    	// Get the category terms
    	$terms = get_the_terms( $post->ID, 'product_cat' );
    	
    	// Only proceed if there is at least 1 category
    	if ( $terms && ! is_wp_error( $terms ) ) {
    	
    		// Get the first term
    		$first_term = $terms[0];
    		 
    		// Get the product note for that term
    		$sd_loop_item_suffix = get_field( 'sd_product_archive_suffix', 'product_cat_' . $first_term -> term_id );
    	
    		// Output the product note
    		echo $sd_loop_item_suffix;
    	
    	}
    	
    }
    
    add_action( 'woocommerce_after_shop_loop_item', 'sd_loop_product_cat_note', 5 );

    This code displays the needed ACF field value in the Shop loop and Category loop in my website. (^_^)

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

You must be logged in to reply to this topic.