target a WooCommerce category's advanced custom field - php

I'm trying to get the advanced custom field for a WooCommerce category. With the following code I get the woocommerce categories:
$categories = get_terms('product_cat');
var_dump($categories);
But why isn't any ACF info included? Is there another function which do gets the ACF info?
UPDATE
This is outside the loop. So I'm trying to get a custom field of a specific product category. I found this info: http://www.advancedcustomfields.com/resources/get-values-from-a-taxonomy-term/
I can't get it to work.
Answer
To get the ACF with get_field(). I needed to use the cat_ID of the array I got with get_categories(). (Maybe it also works with get_terms())
I failed to grasp te second parameter in get_field()
I made it in the following way:
$id = 'product_cat_' . $category->cat_ID;
echo get_field ('field_name', $id);

Based on the documentation of ACF it appears you would get the custom term data as follows:
$category = get_term_by('slug', 'your-category', 'product_cat');
If( ! is_wp_error( $category ) && $custom_field = get_field('your_custom_field', $category ) ){
echo $custom_field;
}

// get the current taxonomy term
$term = get_queried_object();
// vars
$image = get_field('image', $term);
$color = get_field('color', $term);
see documentation here

Related

Woocommerce - Get Attribute Thumbnail and Display

There's a number of posts on here attempting to solve this however, I haven't been able to find a solution that works.
I'm attempting to get the URL of the thumbnail image, associated with the product attribute term of the product displayed on a single-product page, and then display the thumbnail below the product meta, with a URL to the term's archive.
The hook that I'm using is as follows:
add_action('woocommerce_product_meta_end','product_brand_image');
Then I'm defining the function used in the hook:
function product_brand_image() {
}
The attribute name is "Brand", and the slug is "brand".
I've managed to get as far as getting the term ID using the following:
function product_brand_image() {
global $product;
$attributes = $product->get_attributes();
$attr_id = $attributes['pa_brand']['options'][0];
}
I've prnt'd this and it returns the term ID, which I've confirmed to be correct.
I'm struggling to now use this to get the associated term thumbnail URL, and term archive slug to be able to insert the thumbnail and then link it to the archive.
Looking to solve this programmatically without using another plugin.
UPDATE:
Ok, so using #Barrie Dawson comment as a guide, I am now up to this point:
function product_brand_image() {
global $product;
$attributes = $product->get_attributes();
$attr_id = $attributes['pa_brand']['options'][0];
$termMeta = get_term_meta($attr_id);
if (is_array($termMeta) && array_key_exists('product_search_image_id', $termMeta)) {
$post_id = $termMeta['product_search_image_id'][0];
$postData = get_post($post_id);
}
}
I have found that attribute term thumbnails are stored in table wp_posts. The 'product_search_image_id' field links to the ID field in wp_posts table. The URL of the thumbnail associated to the term is then listed under the 'guid' column in this same table. I need some assistance with the PHP to extract this.
<?php
function product_brand_image() {
global $product;
$attributes = $product->get_attributes();
$attr_id = $attributes['pa_brand']['options'][0];
$termMeta = get_term_meta($attr_id);
if (is_array($termMeta) && array_key_exists('thumbnail_id', $termMeta)) {
$termThumbnail = get_the_post_thumbnail_url($termMeta['thumbnail_id']);
}
}
you can also request available sizes see: https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/
And here is the final solution I arrived at:
add_action('woocommerce_product_meta_end','product_brand_image');
function product_brand_image() {
global $product;
$attributes = $product->get_attributes();
$attr_id = $attributes['pa_brand']['options'][0];
$term = get_term($attr_id);
$termSlug = $term->slug;
$termMeta = get_term_meta($attr_id);
if (is_array($termMeta) && array_key_exists('product_search_image_id', $termMeta)) {
$post_id = $termMeta['product_search_image_id'][0];
$postData = get_post($post_id);
$prod_image_url = $postData->guid;
echo '<span class="product_brand_image"><img src="'.$prod_image_url.'" /></span>';
}
}
This displays the thumbnail of the term of the product attribute below the product meta, and links it to the archive page for that term. In this instance, it links to a shop page (archive) with all products associated to a particular 'brand'. See screenshot below.
Screenshot

Get WooCommerce Product Attribute label name

Variable/variations in woocommerce products.
I can do this to get the attribute value from pa_size: <?php echo $product_variation->get_attributes()['pa_size']; ?> which is somewhere in /wp-admin/edit.php?post_type=product&page=product_attributes.
But how do I get the pa_size label (in this case: 'Size')? Have tried to fetch everything based on post_type, page and then the "term_group". But that won't work. I can see that this usually is visible per default, but this is a custom solution. Also in https://github.com/woocommerce/woocommerce/blob/3.8.0/templates/single-product/product-attributes.php I can't see where they print the actual label, only the "Attribute child label and value". But not actual parent (pa_size => Size).
Have googled like a maniac for hours now.
To get the label name of a WooCommerce product attribute you will use one of the 2 following ways:
1) Using Woocommerce wc_attribute_label() dedicated function:
$taxonomy = 'pa_size';
$label_name = wc_attribute_label( $taxonomy );
2) Using wordPress get_taxonomy() function:
$taxonomy = 'pa_size';
$label_name = get_taxonomy( $taxonomy )->labels->singular_name;
In your case, you already know the taxonomy itself pa_size.
All product attributes are just taxonomies, with the pa_ prefix beforehand.
Then you will be able to print them using get_term_by()
it should look something like this:
<?php
$term = get_term_by('slug', $product_variation->get_attributes()['pa_size'], 'pa_size');
echo $term->name;
?>

ACF field display on a page

I've got an issue. I've created a custom field via ACF wordpress plugin. Its a field to custom post type categories (lets say its an additional description to the category). I've tried to add it to my page via such code:
$return_html.= '<h2 class="ppb_menu_title" ';
$return_html.= '>'.$menu_term->name.'</h2><br class="clear"/><br/>';
$displaytitle = the_field('category_subtitle');
$return_html.= '<div class="subtitledesc">'.$displaytitle.'</div>';
below code is a part of full page of code which you can find here [lines 1712-1715]:
https://codeshare.io/50QzqL
what i am doing wrong?
get_field() with a single parameter only works on the current post within the loop iirc, so you will have to provide a target if you're trying to get data for a category.
You'll need the termid of your category (when you're on a taxonomy-page, $term = get_queried_object(); $termid = $term->term_id; should work), then use get_field like so:
get_field( 'category_subtitle', "taxonomyname_$termid" );
get_field( 'category_subtitle', $term ); // if you have a term object
Further reading: https://www.advancedcustomfields.com/resources/get-values-from-a-taxonomy-term/
You'll want to use get_field() instead of the_field() and include the term ID.
get_field() returns a value.
the_field() echoes a value.
Try this: get_field('category_subtitle', 'term_' . $menu_term->term_id)

Get Categories of events in Wordpress Events Calendar Pro Plugin

I'm using Events Calendar Pro plugin (https://theeventscalendar.com/product/wordpress-events-calendar-pro/) and I need to get all categories for each event.
I tried single_cat_title() and get_the_category() but they are not what I needed.
Actually, single_cat_title() function shows only the first category and get_the_category() returns empty array.
You can use following code to get details of each terms.
$cats = get_the_terms( $post_id, 'tribe_events_cat' );
$term = get_term( $cats[1], $taxonomy ); // Getting the 2nd term item
$name = $term->name; //Getting names of terms
More details from https://codex.wordpress.org/Function_Reference/get_term.
Let me know if you have further questions.
I tried single_cat_title() and get_the_category() but they are not what I needed.
get_the_category() function retrieves default categories, post categories. As the categories defined by Events calendar pro plugin is not default, you need to use get_the_terms() function.
In get_the_terms() function, you need to pass post_id as first parameter and taxonomy / category name.
So, wrapping all up you can try below code :-
$event_cats = get_the_terms( $post_id, 'tribe_events_cat' )
Let me know if you need further assistance...
You could try to make a copy of the event.php via the following path:
[your-theme] /tribe/events/v2/list/event.php
and then write the following there:
<?php foreach (get_the_terms(get_the_ID(), 'tribe_events_cat') as $cat) { echo $cat->name; } ?>
And then put it in span/p and styled it.
Inspired from: https://wordpress.stackexchange.com/questions/220511/correct-use-of-get-the-terms

Get Custom Taxonomy ID Assigned To Page

I am trying to get the term ID of the custom taxonomy a page on my website is assigned to.
So I have a page that's assigned to a term "Seventeen" under the custom taxonomy "Clip Category". So I'm looking for the ID of the term seventeen.
Here's the code I've got so far with some help from one kind StackExchange use:
<?php
$slug = $post->post_name;
echo $slug;
$term = get_query_var($slug);
echo $term;
$taxonomy = get_query_var('clipcat');
print_r($taxonomy);
$termid = get_term_by( 'slug', $term, $taxonomy );
echo $term->term_id;
?>
The code gets stuck right after echoing the $slug variable, as can be seen on this page: http://noellesnotes.com/portfolio/seventeen/
You do it wrong. $slug = $post->post_name;is mean you take slug of page, not slug of term.
You should take all terms from page <?php get_the_terms( $post_id, $taxonomy ); ?> and then you can work with it. You will stuck in trouble fell free to ask me. Good luck.
You can use get_query_var to retrieve the public query variable in the WP_Query class of the global $wp_query object
You'll need the term and taxonomy name. The best practice to keep this dynamic is to use get_query_var('term') to get the term slug and get_query_var('taxonomy') to get the taxonomy slug. This will be fed back into get_term_by to get the term ID of the current term being displayed
<?php
$term = get_term_by( 'slug', get_query_var('term'), get_query_var('taxonomy') );
echo $term->term_id;
?>

Categories