1

I used the code here: Adding custom field to product category to add custom fields to my woocommerce category pages.

The used code includes usage to retrieve the data:

echo $productCatMetaTitle = get_term_meta($term_id, 'wh_meta_title', true);
echo $productCatMetaDesc = get_term_meta($term_id, 'wh_meta_desc', true);

Now, I'm not sure how I would pull the contents of each category's 'wh_meta_desc' into the 'woocommerce_before_main_content' hook space on the front-end of each relevant category page?

For example here: https://themirrorman.uk/category/wall-mirrors/

I have the equivalent code that would work to achieve this if I had a custom field named 'wh_meta_title' stored within a PRODUCT page. It would take the contents of 'wh_meta_desc' and place that content into the 'woocommerce_before_main_content' hook space on its product page:

function add_before_main_content() {
   global $post;
   $product_id = $post->ID;

   $productCatMetaTitle = get_post_meta($product_id,'wh_meta_title',true);
   if(!$productCatMetaTitle) return;
   echo ''.$productCatMetaTitle.'';
}
add_action('woocommerce_before_main_content','add_before_main_content');

But how do I adapt this code so that it pulls through 'wh_meta_title' from its newly created CATEGORY page custom field? And then display its contents on the front-end within the 'woocommerce_before_main_content' hook space?

Would my code look something like this?

/** WooCommerce product category custom field - woocommerce_before_main_content - content **/
function add_before_main_content() {
global $post; //Is '$post' correct to apply this function to product category pages?
   $term_id = $term->term_id;
   $productCatMetaTitle = get_term_meta($term_id, 'wh_meta_title', true);
   if(!$productCatMetaTitle) return;
   echo ''.$productCatMetaTitle.'';
}
}
add_action('woocommerce_before_main_content','add_before_main_content');

Thank you.

UPDATE: I've now found a suitable solution here: http://www.wpmusketeer.com/add-a-wysiwyg-field-to-woocommerce-product-category-page/

Works a treat: adding text or html onto woocommerce category pages hookable spaces

Or visit to see it in action now: https://themirrorman.uk/category/wall-mirrors/

VisualWizardry
  • 169
  • 1
  • 2
  • 12

0 Answers0