-
-
Save coenjacobs/2594985 to your computer and use it in GitHub Desktop.
<?php | |
/* | |
Plugin Name: WooCommerce Attribute Links | |
Description: Display product attribute archive links on product page, right below the add to cart button. | |
Version: 1.0 | |
Author: Coen Jacobs | |
Author URI: http://coenjacobs.me | |
*/ | |
add_action( 'woocommerce_product_meta_end', 'cj_show_attribute_links' ); | |
function cj_show_attribute_links() { | |
global $post; | |
$attribute_names = array( '<ATTRIBUTE_NAME>', '<ANOTHER_ATTRIBUTE_NAME>' ); // Insert attribute names here | |
foreach ( $attribute_names as $attribute_name ) { | |
$taxonomy = get_taxonomy( $attribute_name ); | |
if ( $taxonomy && ! is_wp_error( $taxonomy ) ) { | |
$terms = wp_get_post_terms( $post->ID, $attribute_name ); | |
$terms_array = array(); | |
if ( ! empty( $terms ) ) { | |
foreach ( $terms as $term ) { | |
$archive_link = get_term_link( $term->slug, $attribute_name ); | |
$full_line = '<a href="' . $archive_link . '">'. $term->name . '</a>'; | |
array_push( $terms_array, $full_line ); | |
} | |
echo $taxonomy->labels->name . ' ' . implode( $terms_array, ', ' ); | |
} | |
} | |
} | |
} | |
?> |
I created a Folder, and placed the php in a .txt file from above, change line 15 to "pa_brand" "pa_popular-designs"
I don't get an error, but nothing happens, no attributes showing.
Does this code still work with latest woo & wp? would love to use it
Hi Coen
I'm trying to make this into a shortcode, so that if I put it on a page a list of the specified product attributes will show up as links. Is that possible?>
Thanks
Hi Coen
I have the same problem - no error but nothing showing.
Is there something I need to change for more recent versions of woocommerce and WP?
Thanks!
Nope, this code works fine in the latest versions of WooCommerce and WordPress.
Make sure you get the attribute slugs exactly right (including the pa_
prefix) and it will work, just tested it.
Hi,
I confirm, the code works with pa in the prefix of the attributes.
But
How to display on one single line one attribute and then a second line a second attribute
(with the automatic loop)
Hello, this works just fine, but after I click the attribute it shows a white page. I did this stuff here http://wcdocs.woothemes.com/codex/extending/using-custom-attributes-in-menus/ but still not working.
Hi, this is a nifty little "plugin" and it's working flawlessly.
I'm trying to remove the attribute name from the add_action but I've tried everything and it gives me all kinds of different errors. Are there a easy way to change this. Ex.: Buttonsize 3/4 should become Button is 3/4. I would still like the slug to be buttonsize.
Thanks in advance and if this isn't possible it's still a rather nice piece of code :-D
Best regards
Valdimarsson
Well, having not to much PHP-experiance, I would like to know where to put this plugin? Functions.php? A .php-file for by itself? Or where?
I am trying to put an attribute with the property fx: Condition: Used, right next to the SKU in the product_meta div..
Help would be greatfully appreciated!
I can't get this to work. If my attribute name is 'format', I put pa_format (format being the name of the attribute) and they don't show up. Should it be '<pa_format>' or 'pa_format' or pa_format? Neither seem to work. Thanks!
Hello,
From last update of Woocommerce I have label "Product" in front of every attribute name. Please how could I remove it?
Thanks
Change hook
add_action( 'woocommerce_shop_loop_item_title', 'cj_show_attribute_links' );
I want to add Link in attributes Value and want to display Link in attributes Value.
I'm trying to show all terms for an attribute in a list in sidebar, so I think the problem is that $post->ID is for a specific Post. I need to call the entire list (values for the specific attribute)... Any suggestions? Thnaks!