Woocommerce产品详情页:如何将产品状态移至库存状态下方
解决方案:调整WooCommerce产品条件显示位置
要把产品条件移到库存状态下方,核心是移除原有钩子挂载,重新绑定到库存区域之后的合适钩子,具体实现如下:
实现步骤
- 解除原钩子绑定:先移除插件默认的
woocommerce_product_meta_end钩子,避免重复输出 - 重新挂载到目标位置:将产品条件函数绑定到库存显示之后的钩子,确保内容出现在"Availability: In Stock"下方
完整代码示例
将以下代码添加到你的子主题functions.php文件,或者自定义功能插件中:
if ( ! defined( 'ABSPATH' ) ) { exit; } // 移除插件默认的钩子绑定,确保在插件加载完成后执行 add_action( 'init', 'remove_default_product_condition_hook' ); function remove_default_product_condition_hook() { remove_action( 'woocommerce_product_meta_end', 'product_condition_display_product_meta' ); } // 将产品条件挂载到库存状态输出之后的位置 add_action( 'woocommerce_after_stock_html', 'product_condition_display_product_meta', 10 ); // 保留已修改的产品条件输出逻辑(若插件已包含此函数,可省略此部分) function product_condition_display_product_meta() { global $product; $product_condition = product_condition_return_selected_condition( $product->get_id() ); if ( $product_condition ) { $label = apply_filters( 'product_condition_display_label', __( 'Product Condition: ', 'product-condition' ), $product->get_id() ); ?> <span class="product_condition_wrapper" style="display:block; margin-top:8px;"> <span class="product_condition_title"><strong><?php echo esc_html( $label ); ?></strong></span> <span class="product_condition"> <?php switch ( $product_condition ) { case 'other': case 'new-other': $custom_condition = get_post_meta( $product->get_id(), 'product_condition_custom_condition', true ); echo esc_html( $custom_condition ); break; default: // 这里已经是你修改后的Refurbished,无需额外调整 echo esc_html( product_condition_options( $product_condition ) ); break; } ?> </span> </span> <?php } }
关键说明
- 钩子选择:
woocommerce_after_stock_html是最精准的钩子,直接对应库存状态的输出位置之后;如果你的主题自定义了库存显示结构,可改用woocommerce_single_product_summary并设置优先级为25(默认库存显示优先级为20,25会排在其后) - 样式适配:添加了基础行内样式保证排版间距,可根据你的主题样式表调整或移除
- 移除时机:用
init钩子执行移除操作,确保插件完成加载后再解绑,避免移除失败
内容的提问来源于stack exchange,提问作者craig
相关产品推荐
相关产品推荐

