[英]Display specific custom product attributes on single product pages in Woocommerce
I found the following code<\/a> to display all custom attributes on a product detail page (with a specific bar-style design that I need).我发现
以下代码<\/a>可以在产品详细信息页面上显示所有自定义属性(具有我需要的特定条形设计)。 The code works like a charm and I have the proper CSS to display horizontal bars of my custom attributes.
代码就像一个魅力,我有适当的 CSS 来显示我的自定义属性的水平条。
<\/blockquote>
![]()
function isa_woocommerce_all_pa(){ global $product; $attributes = $product->get_attributes(); if ( ! $attributes ) { return; } $out = '<ul class="taste-attributes">'; foreach ( $attributes as $attribute ) { \/\/ skip variations if ( $attribute->get_variation() ) { continue; } $name = $attribute->get_name(); if ( $attribute->is_taxonomy() ) { $terms = wp_get_post_terms( $product->get_id(), $name, 'all' ); \/\/ get the taxonomy $tax = $terms[0]->taxonomy; \/\/ get the tax object $tax_object = get_taxonomy($tax); \/\/ get tax label if ( isset ( $tax_object->labels->singular_name ) ) { $tax_label = $tax_object->labels->singular_name; } elseif ( isset( $tax_object->label ) ) { $tax_label = $tax_object->label; \/\/ Trim label prefix since WC 3.0 if ( 0 === strpos( $tax_label, 'Product ' ) ) { $tax_label = substr( $tax_label, 8 ); } } $out .= '<li class="' . esc_attr( $name ) . '">'; $out .= '<p class="attribute-label">' . esc_html( $tax_label ) . ': '; $tax_terms = array(); foreach ( $terms as $term ) { $single_term = esc_html( $term->name ); \/\/ Insert extra code here if you want to show terms as links. array_push( $tax_terms, $single_term ); } $out .= '<span class="attribute-value">' . implode(', ', $tax_terms) . '<\/span><progress value="' . implode(', ', $tax_terms) . '" max="10"><div class="progress-bar"><span style="width:' . implode(', ', $tax_terms) . '0%">' . implode(', ', $tax_terms) . '<\/span><\/progress><\/li>'; } else { $value_string = implode( ', ', $attribute->get_options() ); $out .= '<li class="' . sanitize_title($name) . ' ' . sanitize_title( $value_string ) . '">'; $out .= '<p class="attribute-label">' . $name . ': '; $out .= '<progress value="' . esc_html( $value_string ) . '" max="10"><\/progress><\/li>'; } } $out .= '<\/ul>'; echo $out; } add_action('woocommerce_single_product_summary', 'isa_woocommerce_all_pa', 20);<\/code><\/pre>"
In the following code you will define first the desired product attributes slugs in an array, that will get displayed in single product pages:在以下代码中,您将首先在数组中定义所需的产品属性 slug,它将显示在单个产品页面中:
add_action( 'woocommerce_single_product_summary', 'display_some_product_attributes', 25 );
function display_some_product_attributes(){
// HERE define the desired product attributes to be displayed
$defined_attributes = array('fyllighet', 'carrier', 'billing-e-number');
global $product;
$attributes = $product->get_attributes();
if ( ! $attributes ) {
return;
}
$out = '<ul class="taste-attributes">';
foreach ( $attributes as $attribute ) {
// Get the product attribute slug from the taxonomy
$attribute_slug = str_replace( 'pa_', '', $attribute->get_name() );
// skip all non desired product attributes
if ( ! in_array($attribute_slug, $defined_attributes) ) {
continue;
}
// skip variations
if ( $attribute->get_variation() ) {
continue;
}
$name = $attribute->get_name();
if ( $attribute->is_taxonomy() ) {
$terms = wp_get_post_terms( $product->get_id(), $name, 'all' );
// get the taxonomy
$tax = $terms[0]->taxonomy;
// get the tax object
$tax_object = get_taxonomy($tax);
// get tax label
if ( isset ( $tax_object->labels->singular_name ) ) {
$tax_label = $tax_object->labels->singular_name;
} elseif ( isset( $tax_object->label ) ) {
$tax_label = $tax_object->label;
// Trim label prefix since WC 3.0
if ( 0 === strpos( $tax_label, 'Product ' ) ) {
$tax_label = substr( $tax_label, 8 );
}
}
$out .= '<li class="' . esc_attr( $name ) . '">';
$out .= '<p class="attribute-label">' . esc_html( $tax_label ) . ': </p> ';
$tax_terms = array();
foreach ( $terms as $term ) {
$single_term = esc_html( $term->name );
// Insert extra code here if you want to show terms as links.
array_push( $tax_terms, $single_term );
}
$out .= '<span class="attribute-value">' . implode(', ', $tax_terms) . '</span><progress value="' . implode(', ', $tax_terms) .
'" max="10"><div class="progress-bar"><span style="width:'
. implode(', ', $tax_terms) . '0%">'
. implode(', ', $tax_terms) . '</span></div></progress></li>';
} else {
$value_string = implode( ', ', $attribute->get_options() );
$out .= '<li class="' . sanitize_title($name) . ' ' . sanitize_title( $value_string ) . '">';
$out .= '<p class="attribute-label">' . $name . ': </p> ';
$out .= '<progress value="' . esc_html( $value_string ) . '" max="10"></progress></li>';
}
}
$out .= '</ul>';
echo $out;
}
Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and works.
测试和工作。
i want to display some custom attributes on the single product page.我想在单个产品页面上显示一些自定义属性。 This is the first code i found that works.
这是我发现的第一个有效的代码。 But i don't want bars to show up but just the text values of the atttributes.
但我不希望显示条,而只是显示属性的文本值。 Could you help me to change the bars to text?
你能帮我把栏改成文字吗? Thanks!
谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.