与此类似但不完全。
我有一个可变订阅产品,可以分 12 个月分期付款。
如果“分期付款次数”为“全额付款”,我希望能够更改 1 个月的 £xxx 文本:
然后将接下来的 11 期付款按 2 个月/3 个月等方式进行。
我知道此代码只会显示价格,但我似乎无法修改它以仅更改 1 个月(全额付款选项):
add_filter('woocommerce_subscriptions_product_price_string_inclusions', 'remove_subscription_inclusions', 10, 2);
function remove_subscription_inclusions( $include, $product ) {
$include['subscription_length'] = '';
$include['subscription_period'] = '';
return $include;
}
我确实尝试过这个但没有任何效果:
add_filter('woocommerce_subscriptions_product_price_string_inclusions', 'custom_subscription_price_string_inclusions', 10, 2);
function custom_subscription_price_string_inclusions( $include, $product ) {
// Check if the product is a subscription product
if ($product->is_type('subscription')) {
// Get the variations of the subscription product
$variations = $product->get_available_variations();
// Loop through each variation to check for the 1-month payment option
foreach ($variations as $variation) {
// Get the variation's price and subscription length/period
$regular_price = $variation['display_price'];
$subscription_length = isset($variation['attributes']['pa_subscription_length']) ? $variation['attributes']['pa_subscription_length'] : '';
$subscription_period = isset($variation['attributes']['pa_subscription_period']) ? $variation['attributes']['pa_subscription_period'] : '';
// Check if this variation is the 1 month (single payment) option
if ('1' === $subscription_length && 'month' === $subscription_period) {
// Modify the price string for the 1 month single payment option
$include['price_string'] = '£' . $regular_price . ' (Full payment for 1 month)';
// Remove subscription length and period to avoid duplication
$include['subscription_length'] = '';
$include['subscription_period'] = '';
}
}
}
return $include;
}
过滤
woocommerce_subscriptions_product_price_string_inclusions
器仅允许您控制最终价格字符串中包含哪些部分,但不允许您轻松更改整个字符串或根据变化进行自定义。如果您想要真正改变价格字符串本身,您应该使用下面的代码。
此过滤器允许您覆盖最终输出。以下版本可以满足您的需求,当选择 1 个月(单笔付款)选项时,会显示“全额付款”,而对于其他选项,则保留通常的“£xx / 月”。
这就是我让它工作的方式:我使用 javascript/jQuery,因为它可以工作,而不管 WooCommerce 内部类的差异。