I have found this coding on here for showing WooCommerce product attributes on a product page: Woocommerce - Display single product attribute(s) with shortcodes in Frontend_
I know there are ways of gettting the attributes sorted within the WooCommerce backend, but I cannot get them to work like they should. I see a lot of people have issues with the sorting of attributes. Is there a way to make them sort by "slug" by modifying this code?
/**
* Attributes shortcode callback.
*/
function so_39394127_attributes_shortcode( $atts ) {
global $product;
if( ! is_object( $product ) || ! $product->has_attributes() ){
return;
}
// parse the shortcode attributes
$args = shortcode_atts( array(
'attributes' => array_keys( $product->get_attributes() ), // by default show all attributes
), $atts );
// is pass an attributes param, turn into array
if( is_string( $args['attributes'] ) ){
$args['attributes'] = array_map( 'trim', explode( '|' , $args['attributes'] ) );
}
// start with a null string because shortcodes need to return not echo a value
$html = '';
if( ! empty( $args['attributes'] ) ){
foreach ( $args['attributes'] as $attribute ) {
// get the WC-standard attribute taxonomy name
$taxonomy = strpos( $attribute, 'pa_' ) === false ? wc_attribute_taxonomy_name( $attribute ) : $attribute;
if( taxonomy_is_product_attribute( $taxonomy ) ){
// Get the attribute label.
$attribute_label = wc_attribute_label( $taxonomy );
// Build the html string with the label followed by a clickable list of terms.
// Updated for WC3.0 to use getters instead of directly accessing property.
$html .= get_the_term_list( $product->get_id(), $taxonomy, '<li class="bullet-arrow">' . $attribute_label . ': ' , ', ', '</li>' );
}
}
// if we have anything to display, wrap it in a <ul> for proper markup
// OR: delete these lines if you only wish to return the <li> elements
if( $html ){
$html = '<ul class="product-attributes">' . $html . '</ul>';
}
}
return $html;
}
add_shortcode( 'display_attributes', 'so_39394127_attributes_shortcode' );
In case anyone were still looking for the answer, if you add
sort($args['attributes']);
before the foreach loop, it will sort the attributes displayed by the attribute slug
Related
I am looking for a way to display a specific attribute on the single Product page of WooCommerce.
I found this code in the folling topic; Shortcode that display all product attributes set for a WooCommerce product
function get_product_attributes_shortcode($atts ) {
// Extract shortcode attributes
extract( shortcode_atts( array(
'id' => get_the_ID(),
), $atts, 'display-attributes' ) );
global $product;
if ( ! is_a($product, 'WC_Product') ) {
$product = wc_get_product( $id );
}
if ( is_a($product, 'WC_Product') ) {
$html = []; // Initializing
foreach ( $product->get_attributes() as $attribute => $values ) {
$attribute_name = wc_attribute_label($values->get_name());
$attribute_data = $values->get_data();
$is_taxonomy = $attribute_data['is_taxonomy'];
$option_values = array(); // Initializing
// For taxonomy product attribute values
if( $is_taxonomy ) {
$terms = $values->get_terms(); // Get attribute WP_Terms
// Loop through attribute WP_Term(s)
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, $attribute );
$option_values[] = ''.$term->name.'';
}
}
// For "custom" product attributes values
else {
// Loop through attribute option values
foreach ( $values->get_options() as $term_name ) {
$option_values[] = $term_name;
}
}
$html[] = '<strong>' . $attribute_name . '</strong>: ' . implode(', ', $option_values);
}
return '<div class="product-attributes">' . implode(' | ', $html) . '<div>';
}
}
add_shortcode( 'display-attributes', 'get_product_attributes_shortcode' );
I can display all attributes by using the shortcode [display-attributes] but I want to display only one specific attribute (in my case the attribute is "Kunstenaar"). However I don't know how to adjust the code to make this work. Anyone who can help me out?
If you need just specific attribute, you don't have to get them all and then go through them. WC have function get_attribute() - documentation
function get_product_attributes_shortcode( $atts ) {
extract( shortcode_atts( array(
'id' => get_the_ID(),
), $atts, 'display-attributes' ) );
global $product;
if ( ! is_a($product, 'WC_Product') ) {
$product = wc_get_product( $id );
}
if ( is_a($product, 'WC_Product') ) {
$kunstenaar = $product->get_attribute( 'Kunstenaar' );
return '<div class="product-attributes"><strong>Kunstenaar</strong>: ' . $kunstenaar . '<div>';
}
}
add_shortcode( 'display-attributes', 'get_product_attributes_shortcode' );
I'm trying to add a specific product attribute ( in this case, the "warranty" ) to the elementor custom product page.
I have tried using "PHP Code Snippets" plugin and adding:
<?php
add_action( 'woocommerce_single_product_summary', 'product_attribute_after_price', 15 );
function product_attribute_after_price () {
global $product;
// HERE add your product attribute SLUG or taxonomy
$attribute_slug = 'guarantee';
$taxonomy = strpos($url, 'blog') !== false ? $attribute_slug : 'pa_' . $attribute_slug;
$attribute_name = get_taxonomy( $taxonomy )->labels->singular_name;
$term_name = $product->get_attribute( $taxonomy ); // The value
if( empty($term_name) ) return; // Exit if empty value
// If not empty we display it
$output = '<div class="product-attribute '.$taxonomy.'"><p>';
$output .= '<strong> '.$attribute_name.'</strong>: ';
$output .= '<span> '.$term_name.'</span>';
echo $output . '</p></div>';
}
but that shows nothing.
what should i do?
I really frustrated, I am a novice in wordpress I am trying to Limit Maximum Numbers of Tag Links to appear within post content article. Below is my code. I don't know how to fix it up.
function link_words( $text ) {
$replace = array();
$tags = get_tags();
$count=0;
if ( $tags ) {
foreach ( $tags as $tag ) {
$count++;
$replace[ $tag->name ] = sprintf( '%s', esc_url( get_term_link( $tag ) ), esc_html( $tag->name ) );
if( $count > 2 ) break;
}
}
$text = str_replace( array_keys($replace), $replace, $text );
return $text;
}
add_filter( 'the_content', 'link_words' );
If your tags are comma-separated, this might work for you:
function limit_tags($tags) {
$tags = substr($tags, 0, strpos($tags, ',', strpos($tags, ',')+1));
return $tags;
}
add_filter('the_tags','limit_tags');
The $tags variable is actually a string...
You mention your function works the way you want it to (linking tags) so I won't mess with that. If you look at the docs for get_tags(), you'll see it accepts a few arguments including number which will limit it. This way you won't have a $counter type variable to mess with.
You can also just check to see if the $tags variable gets set to a truthy value, you don't need to define it first.
Your str_replace is also occurring whether or not $tags is defined, which can cause issues if none are found, so you should move that up into the if statement.
For semantic clarity, I've also changed the $text variable to $content since you're using the_content filter.
add_filter( 'the_content', 'link_tags_in_content' );
function link_tags_in_content( $content ){
if( $tags = get_tags( array( 'number' => 2 ) ) ){
foreach( $tags as $tag ){
$tag_link = sprintf( '%s', esc_url( get_term_link( $tag ) ), esc_html( $tag->name ) );
$content = str_replace( $tag->name, $tag_link, $content );
}
}
return $content;
}
Ok, so I think I understand what you want better now...
function link_words( $text ) {
$tags = get_tags();
if ( $tags ) {
foreach ( $tags as $tag ) {
$from = '!<h2>[^<>]*<\/h2>(*SKIP)(*F)|<b>[^<>]*<\/b>(*SKIP)(*F)|<a\b[^>]*>.*?</a>(*SKIP)(*F)|(\b'.$tag->name.'\b)(?=[^>]*(?:<|$))!';
$to = sprintf( ' %s ', esc_url( get_term_link( $tag ) ), esc_html( $tag->name ) );
$text = preg_replace($from, $to , $text, 2);
}
}
return $text;
}
add_filter( 'the_content', 'link_words' );
Because of the limit on preg_replace (2), and it being inside the tag-loop, this replaces two instances of a tag name in the text for every tag... Is this along the lines of what you want? Be aware that this is not case-insensitive, so it will not match if your tag is the first word in a sentence, and is capitalized. To do something like that, maybe take a look at this thread:
PHP preg_replace: Case insensitive match with case sensitive replacement
I'm using WooCommerce with WordPress to build a store of Antique Photos.
I'm using the WooCommerce product attributes feature to store information about an item photographer.
See here for an example, photographer attribute is pulled out in the box on the left http://bit.ly/1Dp999O
The code that pulls out the attribute looks like this:
if($attribute['is_taxonomy']) {
$values=wc_get_product_terms($product->id, $attribute['name'], array('fields' => 'names'));
echo apply_filters('woocommerce_attribute', wpautop(wptexturize(implode(', ', $values))), $attribute, $values);
}
$values looks like this:
Array ( [0] => Photographer 1 )
The problem is, how do I get to the permalink for the Photographer which is automatically generated by WordPress and WooCommerce:
http://bit.ly/1JtwBna
I can't find any documentation for this within WooCommerce, and this seems to be a taxonomy within a taxonomy which goes a bit further than stabdard WordPress, but I'm assuming this is a fairly standard requirement. Any pointers appreciated.
Once you have the attribute term (in this case the photographer's name) you can use get_term_link() to get the URL. Because the $product id isn't passed to the woocommerce_attribute folder, I couldn't filter that, but instead create an override of the product-attributes.php template. And modified the relevant section to be the following:
if ( $attribute['is_taxonomy'] ) {
$terms = wc_get_product_terms( $product->id, $attribute['name'], array( 'fields' => 'all' ) );
$html = '';
$counter = 1;
$total = count( $terms );
foreach( $terms as $term ){
$html .= sprintf( '%s',
esc_url( get_term_link( $term ) ),
esc_attr( $term->name ),
wptexturize( $term->name )
);
if( $counter < $total ){
$html .= ', ';
}
$counter++;
}
echo wpautop( $html );
}
For some reason, the URL isn't a pretty permalink. It's late and I can't tell if this has to do with my configuration or what exactly, but it is a start.
This basically does what I require:
$terms = get_the_terms($product->id, $attribute['name']);
foreach ($terms as $term){
echo ''.$term->name.'';
}
But could do with a bit of refinement for sure.
I search all the web for that answer. I use wp_list_categories to make a submenu with custom taxonomy, It works well, and puts current-cat when I browse those categories.
The thing is, when I browse single posts with this menu, the highlight no more works.
For the blog part of that site, I use the following code to highlight current category on wp_list_categories():
function sgr_show_current_cat_on_single($output) {
global $post;
if( is_single() ) {
$categories = wp_get_post_categories($post->ID);
foreach( $categories as $catid ) {
$cat = get_category($catid);
if(preg_match('#cat-item-' . $cat->cat_ID . '#', $output)) {
$output = str_replace('cat-item-'.$cat->cat_ID, 'cat-item-'.$cat->cat_ID . ' current-cat', $output);
}
}
}
return $output;
}
add_filter('wp_list_categories', 'sgr_show_current_cat_on_single');
But as far as I tried, can't make it work for single posts that are ordered by custom taxonomy. :/ > I don't know how to custom it.
Is it even possible ?
You need to use get_the_terms( $id, $taxonomy ); instead of wp_get_post_categories(); for getting custom taxonomy term IDs.
You can hardcode taxonomy name into the functon or get it from the $args you passed into wp_list_categories( $args );.
Final code:
add_filter( 'wp_list_categories', 'sgr_show_current_cat_on_single', 10, 2 );
function sgr_show_current_cat_on_single( $output, $args ) {
if ( is_single() ) :
global $post;
$terms = get_the_terms( $post->ID, $args['taxonomy'] );
foreach( $terms as $term ) {
if ( preg_match( '#cat-item-' . $term ->term_id . '#', $output ) ) {
$output = str_replace('cat-item-'.$term ->term_id, 'cat-item-'.$term ->term_id . ' current-cat', $output);
}
}
endif;
return $output;
}