英文:
WooCommerce - change the placement of Attribute on single product page
问题
我正在尝试将一个属性("难度")添加到产品页面的主要列表中。虽然我成功添加了它,但我希望它在"类别"和"标签"之上,而不是在"添加到购物车"按钮下面。请参见截图。
我使用了以下代码(在线找到的),并将其添加到我的functions.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);
英文:
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:
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);
答案1
得分: 1
不需要太久了,所以可能不再需要,但以防万一或者如果其他人想要做同样的事情。你可以这样做:
将:
add_action('woocommerce_single_product_summary', 'njengah_woo_attribute', 25);
改为:
add_shortcode('difficultyAttribute', 'njengah_woo_attribute');
然后,你可以在文本正文中添加短代码[difficultyAttribute]
,放在你想要的任何位置。
英文:
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
答案2
得分: 0
将代码中的部分翻译如下:
将
add_action('woocommerce_single_product_summary', 'njengah_woo_attribute', 25);
更改为
add_action('woocommerce_single_product_summary', 'njengah_woo_attribute', 10);
末尾的数字越小,项目显示得越高。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论