ACF field on Product Attribute show on single product page - php

I'm trying to add a logo to a Woocommerce product attribute (Brand Logo) and then display that brand logo on the Single Product page for each product tagged with that attribute.
I've tried the way I thought it would work however looks far too simple and it returns null.
<?php
function brandLogo(){
global $product;
?><p><?php the_field('brand_logo'); ?></p><?php
}
add_shortcode('brandLogo', 'brandLogo');
?>
I've got the ACF field to return URL of the image and have also tried using get_field as well as the_field.
My php knowledge is quite limited so I can't quite figure out how to get this one sorted.
Cheers.

I have changed the code and learned that it is important to use 'pa_' for product attribute names. So 'pa_yourattribute'. Here is the working code:
<?php
function brandLogo(){
global $product;
$terms = get_the_terms($post->ID , 'pa_brand');
if($terms){
foreach ($terms as $term){
?><img src="<?php the_field('brand_logo', 'pa_brand' . '_' . $term->term_id) ?>"/><?php
}
}
}
add_shortcode('brandLogo', 'brandLogo');
?>

Related

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

Add a custom field variable (ACF) to a custom Woocommerce shortcode

So I'm trying to get a variable to work within an echo do shortcode, however I can't seem to get it working. The goal is to be able to have the client select the brand the backend of the site using ACF instead of hard coding the brand selection.
Here is what I currently have:
<?php $brand_one_ids = get_field( 'brand_one' ); ?>
<?php echo do_shortcode( '[products limit="8" columns="4" class="brand,' . $brand_one_ids . '"]'); ?>
Any advice and fixes would be greatly appreciated!
Nobody could help you on this as you didn't explain in your question, that you:
Have created a product custom taxonomy "Brand".
Are using this answer code to make it work on Woocommerce [products] shortcode
That you want to display those "Brand" products in your home page
Your problem is just a settings problem in ACF. You need to enable those custom fields in your home page, this way:
Then you will get a custom metabox in your home page, with your "Brands" custom fields:
Once filled and saved, this time using the following code on your home, it will work:
<?php if( ! empty( get_field( 'brand_one' ) ) )
echo do_shortcode( '[products limit="8" columns="4" class="brand,' . get_field( 'brand_one' ) . '"]'); ?>

Display Clickable list of a specific taxonomy in my custom post type

I'm having a hard time even coming up with a title for this.
If I'm editing the code for a regulator old blog post (content.php) and add
<?php the_category(', ') ?>
I get a clickable list of all the categories that post belong to.
I have a custom post type called research and a custom taxonomy associated with it called topics. I'm editing content-research.php I just want the topics to show up the same way when looking at a research post.
I tried
<?php the_topic(', ') ?>
and that was a complete failure.
So I'm hoping there is a simple solutions because I have some additional taxonomies for this post type I'd like to add as well.
the_category() function only searches terms from the 'category' taxonomy as you can see in the core file https://core.trac.wordpress.org/browser/tags/4.9.2/src/wp-includes/category-template.php line 75
You could write your own function or you can use this snippet:
$terms = get_the_terms( $post->ID , 'taxonomyname' );
foreach ( $terms as $term ) {
echo ''.$term->name.' ';
}
The answer from ovidiua2003 worked just needed this tweak.
echo ''.$term->name.' ';
So the whole thing would be
<?php
$terms = get_the_terms( $post->ID , 'taxonomy' );
foreach ( $terms as $term ) {
echo ''.$term->name.', ';
}
?>

get_option intermittently fail WordPress

This is my first question.
I have a custom taxonomy set up in a theme inside a functions.php file.
I've added some extra meta fields for the custom taxonomy (category), also set through the functions.php file.
I am using the update_option() function.
Here is the part that is saving the options to the DB:
<?php
// save extra category extra fields hook
add_action ( 'edited_artists', 'save_extra_category_fileds');
// save extra category extra fields callback function
function save_extra_category_fileds( $term_id ) {
if ( isset( $_POST['Cat_meta'] ) ) {
$t_id = $term_id;
$cat_meta = get_option( "category_$t_id");
$cat_keys = array_keys($_POST['Cat_meta']);
foreach ($cat_keys as $key){
if (isset($_POST['Cat_meta'][$key])){
$cat_meta[$key] = $_POST['Cat_meta'][$key];
}
}
//save the option array
update_option( "category_$t_id", $cat_meta );
}
}
?>
In my template file I am calling them like this:
<?php
$terms = wp_get_post_terms( $post->ID, 'artists');
foreach ($terms as $term){
$term_id = $term->term_id;
$term_name = $term->name;
$term_taxonomy_id = $term->term_taxonomy_id;
$term_slug = $term->slug;
//do you term meta stuff here
//print_r($term);
}
?>
This is where I use them (among other things), and it is of course inside the LOOP:
<div class="single-sculpture-artist-info">
<?php
$category_meta = get_option( "category_$term_taxonomy_id");
?>
<a href="<?php echo get_site_url(); ?>/artists/<?php echo $term_slug; ?>">
<img src="<?php echo $category_meta['artists_photo'] ?>" alt="<?php echo $term_name; ?>">
</a>
<h3>
<?php echo $term_name; ?>
</h3>
<p><?php echo $category_meta['artists_city_province'] ?></p>
<p><?php echo $category_meta['artists_bio_excerpt'] ?></p>
</div>
All of this code works perfectly.
I started adding the content, but then it suddenly started to fail. I think it started when I tried to use one of the category (taxonomy) names I used while developing this whole system (my guess is that it was cached somewhere or something), but then I tried using it with different name, and adding some other which were not there before, and it fails as well. My best guess is that somehow the options table is overloaded with data (limit or something).
Is that even possible? I dont have a lot, 56 working posts in that taxonomy, and 34 categories(taxonomy terms).
I tried my best to get my head around it but couldn`t find what was the problem.
When I insert like 2 or 3 more posts, it starts messing around. So, this:
<a href="<?php echo get_site_url(); ?>/artists/<?php echo $term_slug; ?>">
outputs correct link, but, this:
<img src="<?php echo $category_meta['artists_photo'] ?>"
doesnt. It outputs data from some other category (from the same CPT). I can provide additional info upon request.
Not answering to your question but do you know there’s a big change happening with WP 4.2+ where they’re going to be splitting taxonomy terms, so that taxonomy terms don’t share the same term id if their slugs match.
Please take a look at the links below for some details on how this can be addressed
https://make.wordpress.org/core/2015/02/16/taxonomy-term-splitting-in-4-2-a-developer-guide/
https://developer.wordpress.org/plugins/taxonomy/working-with-split-terms-in-wp-4-2/
Through the help of my network I found the solution to the problem. The line where I am calling:
<?php
$category_meta = get_option( "category_$term_taxonomy_id");
?>
I switched to this:
<?php
$category_meta = get_option( "category_$term_id");
?>
This seems to have resolved the problem. However, here are some other solutions that might be more relevant in the similiar situation:
https://en.bainternet.info/tax-meta-class-faq/
also, be sure to read the things Joe Wilson posted above. Thanks again.

Gravity form auto populating field not working

I am using gravity forms for wordpress and and i have following function to auto populate the form field:
add_filter('gform_field_value_vendor_category', 'populate_post_vendor_category');
function populate_post_vendor_category($value){
global $post;
$vendor_category = the_terms( $post->ID, 'listing_category');
return $vendor_category;
}
Adding the parameter name vendor_category to the form does not seem to work.
I have tried the following code on a template file and it displays the current listing category.
<?php global $post;
$vendor_cat = the_terms( $post->ID, 'listing_category');
echo $vendor_cat; ?>
Not sure why the field is not auto populating?
the terms is for echoing . Use get_the_terms() instead for returning values.
get_the_terms( $id, $taxonomy );
as a rule of thumb, when you do not see the prefix get_ in wordpress, the function will echo on the screen. like the_title() ( echoing ) and get_the_title (returning )..

Categories