I'm trying to add an Attribute ("Difficulty") to the main listing of a product page. While I was successful in adding it, I'd like it to be higher and included with the "Category" and "Tags" instead of under the "Add to Cart" button. See screenshot.
我正在尝试向产品页面的主列表项添加一个属性(“难度”)。虽然我成功地添加了它,我希望它更高,并包括在“类别”和“标签”,而不是在“添加到购物车”按钮。请参见屏幕截图。
I used the following code (found online), and added it to my functions.php:
我使用了以下代码(在线找到),并将其添加到我的函数.php中:
function njengah_woo_attribute(){
global $product;
$attributes = $product->get_attributes();
if ( ! $attributes ) {
return;
}
$display_result = '';
foreach ( $attributes as $attribute ) {
if ( $attribute->get_variation() ) {
continue;
}
$name = $attribute->get_name();
if ( $attribute->is_taxonomy() ) {
$terms = wp_get_post_terms( $product->get_id(), $name, 'all' );
$njengahtax = $terms[0]->taxonomy;
$njengah_object_taxonomy = get_taxonomy($njengahtax);
if ( isset ($njengah_object_taxonomy->labels->singular_name) ) {
$tax_label = $njengah_object_taxonomy->labels->singular_name;
} elseif ( isset( $njengah_object_taxonomy->label ) ) {
$tax_label = $njengah_object_taxonomy->label;
if ( 0 === strpos( $tax_label, 'Product ' ) ) {
$tax_label = substr( $tax_label, 8 );
}
}
$display_result .= $tax_label . ': ';
$tax_terms = array();
foreach ( $terms as $term ) {
$single_term = esc_html( $term->name );
array_push( $tax_terms, $single_term );
}
$display_result .= implode(', ', $tax_terms) . '<br />';
} else {
$display_result .= $name . ': ';
$display_result .= esc_html( implode( ', ', $attribute->get_options() ) ) . '<br />';
}
}
echo $display_result;
}
add_action('woocommerce_single_product_summary', 'njengah_woo_attribute', 25);
更多回答
优秀答案推荐
It's been a while so probably not needed anymore, but just in case or if someone else wants to do the same. You can do this:
它已经有一段时间了,所以可能不再需要了,但只是以防万一,或者如果其他人想要做同样的事情。您可以执行以下操作:
Change:
更改:
add_action('woocommerce_single_product_summary', 'njengah_woo_attribute', 25);
to
至
add_shortcode( 'difficultyAttribute', 'njengah_woo_attribute');
Then you can add the shortcode [difficultyAttribute]
in the body text wherever you want to place it
然后,您可以在正文文本中想要放置它的任何位置添加短码[困难属性
Change
变化
add_action('woocommerce_single_product_summary', 'njengah_woo_attribute', 25);
to
至
add_action('woocommerce_single_product_summary', 'njengah_woo_attribute', 10);
The smaller the number at the end, the higher the item will be displayed.
末尾的数字越小,项目显示的位置就越高。
更多回答
I just tried that, but no change.
我刚试过了,但没什么变化。
我是一名优秀的程序员,十分优秀!