Get WooCommerce Product Attribute label name - php

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;
?>

Related

Disable WooCommerce Add to Cart button for a specific "Brand" Custom Field value

I have a coding problem that is just out of my capacity.
I have setup a WooCommerce store with WooCommerce Brands plugin.
I need to be able to "Close" a brand by disabling the "Add to Cart" button. I don't want it to disappear, I only want to disable the button.
I have created a custom field using Advanced Custom Fields and assigned it to the "Brands" taxonomy which is "product_brand".
My custom field is: close_store
Type: Checkbox
Options: Open (Default value) | Closed
When I go to edit a "Brand" I can see my custom field and when I select "Closed" I need a function to then disable the "Add to Cart" buttons.
Is there anyone who can help with this ?
Possible Cross Reference:
Disabling Add to Cart Button for Specific WooCommerce Products
The above looks to do a similar thing but uses "Labels" as the closing criteria and not a custom field. There may be some cross reference here in terms of how the function may need to work.
Hope this helps!
--------------------------------->
UPDATE - Possible Help
Provided by ACF Theme Code Pro Plugin
Taxonomy Term Variables
<?php
// Define taxonomy prefix eg. 'category'
// Use 'term' for all taxonomies
$taxonomy_prefix = 'product_brand';
// Define term ID
// Replace NULL with ID of term to be queried eg '123'
$term_id = NULL;
// Example: Get the term ID in a term archive template
// $term_id = get_queried_object_id();
// Define prefixed term ID
$term_id_prefixed = $taxonomy_prefix .'_'. $term_id;
?>
<?php $close_store_checked_values = get_field( 'close_store', $term_id_prefixed ); ?>
<?php if ( $close_store_checked_values ) : ?>
<?php foreach ( $close_store_checked_values as $close_store_value ): ?>
<?php echo esc_html( $close_store_value ); ?>
<?php endforeach; ?>
<?php endif; ?>

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)

How to show more than 1 custom taxonomy term or link on a custom post type?

I have created a custom taxonomy called 'tema' and the taxonomy has three terms. I want to show ALL the term links that are related to the current post. Currently I can only get my code to show ONE of the posts taxonomy terms...
I would like the term links displayed via my custom content.php file ("content-home.php") that I use for showing excerpts of my custom posts on my homepage.
Currently I have this code placed in my custom content.php file and it actually works fine but I can only get it to show ONE term:
<?php
$terms = get_the_terms( $post->ID, 'tema');
foreach($terms as $term) {
echo '<span>' . $term->name . '</span>';
}
?>
Can anyone please show me how I get it to show ALL the posts taxonomy term links?
In the WordPress Codex you can find:
For get_the_terms:
"Retrieve the terms of the taxonomy that are attached to the post."
http://codex.wordpress.org/Function_Reference/get_the_terms
For get_terms:
"Retrieve the terms in a taxonomy or list of taxonomies."
http://codex.wordpress.org/Function_Reference/get_terms
So, get_the_terms() will get the terms (e.g. categories) attached to a post, whereas get_terms() will retrieve the terms in a taxonomy (e.g. categories in the category taxonomy). For example, get_terms( 'category' ) will return all categories that you have added to your WordPress site.
You should use something like this:
<?php
$terms= get_terms(array('taxonomy'=>'tema'));
foreach($terms as $term){
echo '<span>' . $term->name . '</span>';
}
?>
Try below hook function to get list of taxonomy of particular post id,
//Returns All Term Items for "my_taxonomy"
$term_list = wp_get_post_terms($post->ID, 'my_taxonomy', array("fields" => "all"));
print_r($term_list);
*my_taxonomy - replace your taxonomy
https://codex.wordpress.org/Function_Reference/wp_get_post_terms

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

target a WooCommerce category's advanced custom field

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

Categories