【问题标题】:How to replace a string in Woocommerce single product variation description?如何替换 Woocommerce 单一产品变体描述中的字符串?
【发布时间】:2017-05-09 17:00:05
【问题描述】:

有人可以建议我如何通过 WooCommerce 中单个产品变体描述的操作或过滤器(或操作标记)来进行 PHP 字符串替换吗?

似乎没有任何挂钩。

以上是为了让我可以将描述的每个换行符变成一个列表项。

【问题讨论】:

    标签: php wordpress woocommerce product variations


    【解决方案1】:

    您可以尝试在自定义挂钩函数中使用 Wordpress 专用的 gettex 挂钩,这样:

    add_filter('gettext', 'wc_custom_renaming', 10, 3);
    function wc_custom_renaming( $replaced_text, $source_text, $domain ) {
        // only for single product pages
        if( is_product() ) {
            // Set here the complete description to be replaced
            if( $source_text == 'The text to replace' ){
                // Set here your replacement description
                $replaced_text = __( 'Your new text',$domain );
            }
        }
        return $replaced_text;
    }
    

    代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

    这仅在您替换完整描述时才有效。如果您只想替换一个单词或一个子字符串,您需要使用 strpos()str_replace() php 函数进一步自定义此函数...

    【讨论】:

    • 谢谢,我不知道这个过滤器。我需要这个来专门针对产品变体的描述,并将“-”替换为“
    • ”。你能告诉我如何修改你原来的建议来适应这个吗?
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签