You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

移除WooCommerce订阅下拉菜单中的价格与折扣文本

解决WooCommerce订阅下拉选项移除价格及折扣信息的问题

你需要移除订阅下拉选项中的“for”字样、价格和折扣信息,仅保留周期文本,但之前使用的钩子未生效——原因是你用的钩子并不适用于WooCommerce Subscriptions Add-Ons生成的下拉选项(即带有wcsatt-options-product-dropdown类的select)。这类下拉的标签文本由专门的钩子控制,以下是可行的解决方案:

解决方案代码

使用wcsatt_product_subscription_option_label钩子直接修改下拉选项的标签内容,提供两种实现方式:

方式1:直接构建周期文本

不依赖原标签内容,根据周期参数生成干净的目标文本:

function custom_wcsatt_subscription_option_label( $label, $product_id, $interval, $period, $price ) {
    // 处理单数/复数周期文本(比如1 month而非1 months)
    $period_text = $interval == 1 ? $interval . ' ' . rtrim( $period, 's' ) : $interval . ' ' . $period;
    // 生成最终文本
    $clean_label = 'Delivery every ' . $period_text;
    
    return $clean_label;
}
add_filter( 'wcsatt_product_subscription_option_label', 'custom_wcsatt_subscription_option_label', 10, 5 );

方式2:从原标签中截取有效内容

基于原标签文本,用正则表达式移除价格和折扣部分:

function custom_wcsatt_subscription_option_label( $label ) {
    // 移除从"for"开始到结尾的所有内容(包括价格、折扣)
    $clean_label = preg_replace('/\s+for\s+.*/', '', $label);
    // 添加"Delivery"前缀
    $clean_label = str_replace('Every', 'Delivery every', $clean_label);
    
    return trim($clean_label);
}
add_filter( 'wcsatt_product_subscription_option_label', 'custom_wcsatt_subscription_option_label', 10, 1 );

为什么之前的代码无效?

你之前使用的woocommerce_subscriptions_product_price_string和woocommerce_subscription_price_string钩子,仅用于修改产品页面上的订阅价格展示文本,而非订阅下拉选项的标签内容。WooCommerce Subscriptions Add-Ons的下拉选项由wcsatt_product_subscription_option_label钩子单独控制,因此之前的钩子无法影响到下拉选项的文本。

效果验证

替换代码后,订阅下拉选项会生成符合目标的HTML,仅保留“Delivery every month”“Delivery every 2 months”这类周期文本,完全移除价格和折扣信息。

内容的提问来源于stack exchange,提问作者Chris Brennan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 14:47:32