在WooCommerce中,我使用的是WooCommerce订阅插件,我正在try 将单个产品页面中的订阅价格字符串中的"每3天"替换为"每周两次".

这是我的代码try :

function replace_single_product_table_text( $content ) {

    if ( is_product() ) {

        preg_match_all( '/<table[^>]*class="[^"]*shop_table[^"]*"[^>]*>(.*?)<\/table>/s', $content, $matches );


        foreach ( $matches[0] as $match ) {
            $new_content = str_replace( 'every 3 days', 'Twice a Week', $match );
            $new_content = str_replace( '/ week', '/ Week', $new_content );
            $new_content = str_replace( 'one a Week', 'Once a Week', $new_content );
            $content = str_replace( $match, $new_content, $content );
        }
    }
    return $content;
}
add_filter( 'the_content', 'replace_single_product_table_text', 99 );

不幸的是,它根本不起作用.

任何帮助都将不胜感激.

推荐答案

您没有使用Wright钩子,您的代码可以简化.try 以下操作:

add_filter( 'woocommerce_subscriptions_product_price_string', 'subscr_product_price_string_replacement', 10, 3 );
function subscr_product_price_string_replacement( $subscription_string, $product, $include ){
    $original_string    = "every 3 days";
    $replacement_string = "Twice a Week";

    if ( strpos($subscription_string, $original_string) !== false ) {
        $subscription_string = str_replace( $original_string, $replacement_string, $subscription_string );
    }
    return $subscription_string;
}

代码位于您的子主题的unctions.php文件中(或在插件中).经过测试,效果良好.

Php相关问答推荐

PHP FFI—将PHP数组转换为C指针数组

Laravel集合块()方法

ForHumans函数中的月当量与单位限制

Codeigniter 3中SMTP.office 365.com的截断问题

如何将WooCommerce产品从一个产品复制到另一个产品S

限制联系人页面仅允许在WooCommerce中登录的用户

WooCommerce在收据页面获取产品下载URL

Symfony框架锁定和.env设置

WooCommerce:如果购物车中已存在产品,则增加购物车商品数量

如何在 Laravel 中app文件夹之外的文件夹内创建文件路径?

用自定义代码替换 WooCommerce 单一产品简短描述

在 vue 自定义渲染器中访问变量

PHP:如何将多列sql查询结果转换成数组

如何从此字符串创建 Carbon 对象:2022-06-21T05:52:46.648533678Z?

PHP / DOM : 解析 HTML 以提取基于类的数​​据

是否有适用于 Windows 的真正 64 位版本的 PHP 5.6?

从 CSV 文件 seeder 数据库时的额外行

Laravel Websockets 错误:(WebSocket 在建立连接之前关闭)

为什么可以从 PHP 类访问 PHP Trait 的私有成员?

PHP - 如何获取发送到脚本的查询参数