Support

Account

Home Forums General Issues Product Category extra description

Solving

Product Category extra description

  • Hi,

    I have added a an extra description under the Product category (product_cat) like a WYSIWYG input field.
    So far soo good. I named it “extra description”

    Ive installed a snippet plugin to add the php code.

    Now i want to display this under the product category page in position “woocommerce_after_main_content”

    So how will this code look like? Tried several php-snippets but it wont work , pleeease help.

    My two cent, would be fantastic if this plugin would be able to add shortcodes so we just could put in easier. Thanks!

  • You would need to do something like

    
    add_action('woocommerce_after_main_content', 'my_extra_description');
    function my_extra_description() {
      // get the WC product ID
      // Please note that this is a guess and that
      // you may need to get someone that know WC
      // if it does not work
      $product = wc_get_product();
      $id = $product->get_id();
      // get categories for product
      $cats = get_object_terms($id, 'product_cat');
      // loop over cats
      if ($cats) {
        foreach ($cats as $cat) {
          // get acf field from cat
          $value = get_field('extra_description', $cat);
        }
      }
    }
    
  • Hello everybody
    I had the same question. Do I have to add this code into the woocommerce/archive-product.php file? If so, between which lines?

    Thank you for your help.

  • The code John has kindly added should go in your functions file.

  • Sorry, it does not work for me…
    I have exactly the same need than @peterpans
    I created the this rule: Taxonomy is equal Category (product_cat), then I am able to provide some content for each product category. But when I try to add some php code to display it, I have no good result, as I am not a developer and tutorials I found are not detailed enough…

  • Hi, did anyone managed to solve this issue? We cannot display any ACF values on product category pages. The code provided above does not work.

  • Does something like the below work:

    add_action( 'woocommerce_after_main_content', 'my_extra_description' );
    function my_extra_description() {
    
    	global $wp_query;
    
    	# get the query object
    	$category = $wp_query->get_queried_object();	
    	#get the cat ID
    	$category_ID  = $category->term_id;
    	#get the field
    	$extra_description = get_field( 'extra_description', 'category_'.$category_ID );
    	#if we have data, show it
    	if( $extra_description ){
    		echo $extra_description;
    	}
    	
    }

    Code untested and goes in your functions file

  • Thank you @Jarvis.
    I added this code at the end of the functions.php file of the main theme of the website, and replaced every “extra_description” characters strings by the name of my field (“contenu_categorie”) (except in the 2 first lines of the code of course).
    … Nothing has changed in the front end, even if I clear the cache (of course, I added some contents in my fields from the back-office)…

  • Hey @jarvis thanks for the help! I have also tested it and it doesn’t seem to work. Also are you sure that the code is supposed to work for product categories? Keeping in mind that it’s under a post type of product and taxonomy product_cat, I don’t think it’s the correct approach.

    Please take a look at these pathways:

    Product categories: /wp-admin/edit-tags.php?taxonomy=product_cat&post_type=product
    Specific product category: /wp-admin/term.php?taxonomy=product_cat&tag_ID=3449

    Thank you again. It’s much appreciated!

  • Hi @websteem

    Odd, if you use the below, what do you see?

    add_action( 'woocommerce_after_main_content', 'my_extra_description' );
    function my_extra_description() {
    
    	global $wp_query;
    
    	# get the query object
    	$category = $wp_query->get_queried_object();	
    	#get the cat ID
    	$category_ID  = $category->term_id;
    	
    	echo '<p>Can we see this? Is '.$category_ID.' the right ID for this category?</p>'; #remove after debug
    	
    	#get the field
    	$contenu_categorie = get_field( 'contenu_categorie', 'category_'.$category_ID );
    	#if we have data, show it
    	if( $contenu_categorie ){
    		echo $contenu_categorie;
    	}
    	
    }

    I’ve just tested the above with your field name on the twenty-twentyone theme and worked

  • Still nothing…
    The code is there, but nothing is displayed under the products grid (https://fleursdemontagne.com/categorie-cbd/huile-cbd/ for example)
    I thought that some code should be integrated in the archive-product.php (https://woocommerce.github.io/code-reference/files/woocommerce-templates-archive-product.html). Wrong track?

  • Hello everybody

    Here is the answer I received this morning (French time) of Said, from the official ACF support:

    With the field’s Location rule set as Taxonomy is equal to product_cat, the field should appear on your individual product category edit page. With the code below added in functions.php, the field value displays just fine on the individual category archive in the frontend as shown in this recording: https://www.awesomescreenshot.com/video/5711245?key=aa4a06a3a6ab345566493738b7aad9d5

    add_action( ‘woocommerce_after_main_content’, ‘extra_description’ );
    function extra_description() {

    global $wp_query;

    $product_cat = $wp_query->get_queried_object();
    $cat_ID = $product_cat->term_id;

    $extra_info = get_field( ‘product_extra’, ‘category_’ . $cat_ID );

    if( $extra_info ){
    echo $extra_info;
    }
    }

    So i will try it out and let you know how it goes.

  • @websteem that’s pretty much the same code I gave you!

    Are you adding the code to your functions file?

    You can add code to the archive-product.php template, it won’t be the code supplied though as you asked to use the woocommerce hooks.

    If you want to add direct to the template, just use:

    	global $wp_query;
    
    	# get the query object
    	$category = $wp_query->get_queried_object();	
    	#get the cat ID
    	$category_ID  = $category->term_id;
    	
    	echo '<p>Can we see this? Is '.$category_ID.' the right ID for this category?</p>'; #remove after debug
    	
    	#get the field
    	$contenu_categorie = get_field( 'contenu_categorie', 'category_'.$category_ID );
    	#if we have data, show it
    	if( $contenu_categorie ){
    		echo $contenu_categorie;
    	}
  • So far, Said’s code doesn’t display anything in my website. Maybe the theme is probably responsible of this situation.
    Have a look to what I did : https://www.awesomescreenshot.com/video/5721907?key=4153c64b2fa5aa641bab9725fe3e7c45
    I am doing something wrong?


    @jarvis
    About the code which could go into the archive-product.php file, I guess there is a very precise location in the existing code where to add this extra code?

  • Hi @websteem

    Looking at your video, it seems you’re using a child theme (Handmade Child Theme) but then update Handmade: functions.php not Handmade Child Theme: functions.php

    So that could be the first issue.

    Try adding the code there instead. If it still doesn’t work, switch to the default twenty-twentyone theme and add the below to functions.php:

    
    add_action( 'woocommerce_after_main_content', 'my_extra_description' );
    function my_extra_description() {
    
    	global $wp_query;
    
    	# get the query object
    	$category = $wp_query->get_queried_object();	
    	#get the cat ID
    	$category_ID  = $category->term_id;
    	
    	echo '<p>Can we see this? Is '.$category_ID.' the right ID for this category?</p>'; #remove after debug
    	
    	#get the field
    	$contenu_categorie = get_field( 'contenu_categorie', 'category_'.$category_ID );
    	#if we have data, show it
    	if( $contenu_categorie ){
    		echo $contenu_categorie;
    	}
    	
    }

    At the very least, when you go to your shop category page, it should show ‘Can we see this? Is…’

    Then perhaps you have an issue with your theme!

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

You must be logged in to reply to this topic.