【问题标题】:How to change multiple subscription products strings in woocommerce subscriptions?如何在 woocommerce 订阅中更改多个订阅产品字符串?
【发布时间】:2019-12-17 20:52:06
【问题描述】:

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

我正在运行此代码,该代码运行良好(将“月更改为 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' );

【问题讨论】:

  • 嗨@Imran!你在解决这个问题上的尝试是什么?你能发布你迄今为止尝试过的东西吗?你知道你想用什么替换什么字符串吗?谢谢!

标签: php wordpress woocommerce str-replace woocommerce-subscriptions


【解决方案1】:

您可以选择替换所有您需要的字符串鉴于您知道它所说的现在

我找不到文档,但考虑到 $pricestring 具有值 biweeklyweeklyyearly,我会遵循该模式并尝试:

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 中意外重写weekly,请注意biweeklyweekly 的顺序。

【讨论】:

  • 感谢您的评论@Kit O,我之前也做过同样的事情,但它仅适用于 1 个参数而不是全部三个。
  • 不确定,这是什么意思。我认为您本质上需要知道当前值是多少,以便能够替换它。也许你可以阅读laurena.blog/collecting-donations-with-woocommerce,看看是否有帮助。谢谢!
  • 查看文章和文档:docs.woocommerce.com/document/… 看起来替换最后一个词应该可以。你试过输入"week""year",@ImranIrshad吗?
【解决方案2】:

只需进行类似的替换,您甚至可以嵌入它们,例如:

function wc_subscriptions_custom_price_string( $pricestring ) {
    $newprice = str_replace(
        'month', 
        '30 days', 
        str_replace(
            'year',
            '365 days',
            str_replace(
                'week',
                '7 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' );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-17
    • 2017-06-18
    • 2018-08-31
    • 2021-01-25
    • 2017-12-03
    • 2019-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多