'Woocommerce - Display single product attribute(s) with shortcodes in Frontend
i've read many Q/A's here during the last few days, but unfortunately none of them solved my issue.
I'm trying to fetch product attributes and display them on the frontend with a shortcode. I have managed to display ALL available attributes and display them in a list, but i need to select only one or two of them in different locations (thats why using shortcodes). For example like [shortcode_attribute name="brand"].
Any help is highly appreciated!
my code so far:
function tutsplus_list_attributes( $product ) {
global $product;
global $post;
$attributes = $product->get_attributes();
if ( ! $attributes ) {
return;
}
foreach ( $attributes as $attribute ) {
// Get the taxonomy.
$terms = wp_get_post_terms( $product->id, $attribute[ 'name' ], 'all' );
$taxonomy = $terms[ 0 ]->taxonomy;
// Get the taxonomy object.
$taxonomy_object = get_taxonomy( $taxonomy );
// Get the attribute label.
$attribute_label = $taxonomy_object->labels->name;
// Display the label followed by a clickable list of terms.
echo get_the_term_list( $post->ID, $attribute[ 'name' ] , '<div><li class="bullet-arrow">' . $attribute_label . ': ' , ', ', '</li></div>' );
}
}
add_action( 'woocommerce_product_meta_end', 'tutsplus_list_attributes' );
add_shortcode('display_attributes', 'tutsplus_list_attributes');
Solution 1:[1]
If anyone else just wants to simply output the attribute value as text, replace the $html .= line with this:
$html .= strip_tags(get_the_term_list($product->get_id(), $taxonomy));
@helgatheviking There must be a more elegant way than this! :)
Solution 2:[2]
Needed this recently - found https://wordpress.org/plugins/wcpas-product-attributes-shortcode/ does the trick.
With this plugin you can set an "attribute" parameter to match one of the product attributes you have setup.
For example if you have a brand attribute you can use:
[wcpas_product_attributes attribute="brand"]
This will output a <ul> list containing all brands.
There are also a number of parameters you can use alongside the attribute including:
- orderby (order the list by an orderby value)
- order (order asc, desc, rand, etc)
- hide_empty (hide empty terms)
- show_counts (show the total number of products in the brand)
- archive_links (use an archive based URL instead of a filter based URL)
- min_price (When using archive_links = 0 you can have a price filter based URL off a minimum price)
- max_price (When using archive_links = 0 you can have a price filter based URL off a maximum price)
Solution 3:[3]
The "Work in progress" error could mean that you are not passing the correct user id in your views.publish call. Perhaps you are hard-coding the user id from your initial Workspace? Keep in mind that your user id is different in each Workspace. You will also need to extract the user id dynamically in order for other users to see the App Home content.
The best practice here would be to use the Events API and listen for the app_home_opened event. With the response from this event you can then use the user_id to display the view in the App Home for each user that accesses it.
Solution 4:[4]
When you are creating an app under an workspace you can use the current workspace to develop and test the app under all the circumstances.
However, if you want to port or install you Slack app in another workspace you need to install the app in the workspace using the app installation credentials from that workspace. To be specific of your App Home showing 'Work under progress' there may be a few reasons why that shows up:
- Check the values of bot token, user id and view payload modal used under the
views.publishmethod. - Toggle the Home Tab on and off, then re-install the app to workspace.
- Check the installation parameters of your Slack Application in the new workspace.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Disloxic |
| Solution 2 | |
| Solution 3 | sandra |
| Solution 4 | naren_srini |
