
正如摘要所说,本文将重点介绍如何扩展代码,使其能够支持多个产品id,从而实现更灵活的价格展示控制。
以下代码展示了如何为特定的 WooCommerce 产品 ID 添加自定义前缀或后缀。该示例使用了 woocommerce_get_price_html 钩子,并结合 in_array() 函数来支持多个产品 ID。
add_filter( 'woocommerce_get_price_html', 'bbloomer_price_prefix_suffix', 100, 2 );
function bbloomer_price_prefix_suffix( $price, $product ){
// 产品 ID 列表
$ids = array(555, 556, 557);
if( in_array($product->get_id(), $ids)) {
$price = '<span class="custom-prefix">特别价格:</span>' . $price . '<span class="custom-suffix"> (促销)</span>';
} else {
$price = $price . ' <span class="std-suffix">元</span>';
}
return $price;
}代码解释:
通过使用 woocommerce_get_price_html 钩子和 in_array() 函数,可以轻松地为 WooCommerce 产品价格添加自定义前缀和后缀,并支持多个产品 ID。这种方法灵活且易于扩展,可以满足各种自定义价格显示的需求。在实际应用中,请注意代码的性能和可维护性,并根据需要进行优化。
以上就是为 WooCommerce 产品价格添加自定义前缀/后缀(支持多个产品 ID)的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号