Woocommerce - Get Attribute Thumbnail and Display - php

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

Related

Woocommerce change default product images to their category image

So, here is the woocommerce setting that I have.
I have a few categores, let’s call them A, B, C, D, E… and so on.
From the backend, I gave category images to each categories.
Now, let’s say I post a product in the category A without selecting any images.
The product just shows the woocommerce default image (the one with the grey background).
Is there a way to use the category images as the default product image when there is no image selected?
I have tried a few codes, this is the last one I have:
add_action( 'init', 'custom_fix_thumbnail');
function custom_fix_thumbnail(){
add_filter('wc_placeholder_img_src', 'custom_woocommerce_placeholder_img_src');
function custom_woocommerce_placeholder_img_src( $src ) {
if (is_shop() || is_singular('product') || is_archive() || is_checkout() || is_cart()) {
global $post;
$array = get_the_terms($post->ID, 'product_cat');
reset($array);
$first_key = key($array);
$thumbnail_id = get_woocommerce_term_meta($first_key, 'thumbnail_id', true);
// get the image URL for parent category
$image = wp_get_attachment_url($thumbnail_id);
// print the IMG HTML for parent category
if ($image)
$src = $image;
}
return $src;
}
}
Try changing hook to woocommerce_placeholder_img_src. wc_placeholder_img_src seems to be deprecated.

Displaying content of product category custom field on category page front-end

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:
Or visit to see it in action now: https://themirrorman.uk/category/wall-mirrors/

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

How to create a PHP function to return a product category description - woocommerce

I'm newby in php, and I need some help.
I'm using wp al export to export data as product title, category, excerpt, etc...
They have an option to export the produtc_category title, but not the product category description.
They have an option to export the category value returned by a PHP function, and the value I have to return is the product category description.
Example
I have to create a PHP function to get that value and I'm getting crazy to achieve that.
I found a post that explains how to show the product category description in the product category page
add_action( 'woocommerce_after_subcategory_title', 'custom_add_product_description', 12);
function custom_add_product_description ($category) {
$cat_id = $category->term_id;
$prod_term = get_term($cat_id,'product_cat');
$description= $prod_term->description;
echo '<div>'.$description.'</div>';
}
The thing is that I don't want to display the product category description in my product category page. I just want to create a function to return the description when it is called.
Please HELP!!!!! :)
Try this may be you get some solution:
add_action( 'woocommerce_after_shop_loop_item_title', 'my_add_cat_description', 9);
function my_add_cat_description () {
/* if(!is_shop()){*/ //check whether execute shop page?
global $product;
$terms = get_the_terms( $product->ID, 'product_cat' );
foreach ($terms as $term) {
echo $product_cat_id = $term->description;
break;
}
echo '<div class="shop_cat_desc">testing</div>';
/*}*/
}
visit link to check condition: LINK

How to add icon next to WP post title based on custom field

I am trying to add a little icon (jpg) next to every WordPress posts' titles that have a specifc custom field. Anyone would know how to do it?
I have defined a custom field for some posts like this: $custom = 2 and i want these posts' titles to include an icon.
So to access the specific custom field, use this:
get_post_meta($post_id, $key, $single);
Where $post_id = the id of the post (use
$post->ID
to get a post's ID)
Where $key = the name of the custom field (ie. "custom")
where $single = If set to true then the function will return a single result, as a string
So you'd use something like:
$key = 'custom';
if (get_post_meta($post->ID, $key, true) == 2){
//echo icon here
}
You can also check out Wordpress's article on it: http://codex.wordpress.org/Custom_Fields

Categories