首页 > 解决方案 > 如何在 woocommerce 订阅中更改多个订阅产品字符串?

问题描述

我的网站上有多个订阅产品,每月、每年和每周。

我正在运行此代码,它工作正常(将“月更改为 30 天”)但仅适用于具有“每月”的产品我如何应用它来更改每周和每年的字符串?

谢谢

function wc_subscriptions_custom_price_string( $pricestring ) {
    $newprice = str_replace( 'month', '30 days', $pricestring );
    return $newprice;
}
add_filter( 'woocommerce_subscriptions_product_price_string', 'wc_subscriptions_custom_price_string' );
add_filter( 'woocommerce_subscription_price_string', 'wc_subscriptions_custom_price_string' );

标签: phpwordpresswoocommercestr-replacewoocommerce-subscriptions

解决方案


你可以选择替换所有你需要的字符串,只要你知道,它现在说什么。

我找不到文档,但考虑$pricestring到 values biweekly, weekly, yearly,我会遵循模式,并尝试:

function wc_subscriptions_custom_price_string( $pricestring ) {
    $newprice = str_replace( 'biweekly', '14 days', $pricestring );
    $newprice = str_replace( 'weekly', '7 days', $pricestring );
    $newprice = str_replace( 'yearly', '365 days', $pricestring );
    return $newprice;
}

基本上,它接受$pricestring并替换里面找到的任何文本(第一个参数),并用任何文本(第二个参数)替换它,再次保存并返回修改后的$newprice.

如果你biweekly想避免在.weeklyweeklybiweekly


推荐阅读