Advertisement
lorro

WooCommerce - Make attributes into links

Jun 6th, 2015
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.85 KB | None | 0 0
  1. <?php
  2.   // make attributes into links
  3.   // code goes in functions.php for your child theme
  4.   add_filter ('woocommerce_attribute', 'link_attributes', 10, 3);
  5.   function link_attributes($attributes_string, $attribute, $terms) {
  6.     global $post;
  7.     $taxonomy = get_taxonomy( $attribute['name'] );
  8.     if ( $taxonomy && ! is_wp_error( $taxonomy ) ) {
  9.       $attribute_string = '';
  10.       $terms = wp_get_post_terms( $post->ID, $taxonomy->name );
  11.       if ( !empty( $terms ) ) {
  12.         foreach ( $terms as $term ) {
  13.           if (strlen($attribute_string) > 0) {
  14.             $attribute_string .= ', ';
  15.           }
  16.           $archive_link = get_term_link( $term->slug, $attribute['name'] );
  17.           $attribute_string .= '<a href="' . $archive_link . '">'. $term->name . '</a>';
  18.         }
  19.       }
  20.     }
  21.     return '<p>'.$attribute_string.'</p>';
  22.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement