首页 > 解决方案 > 在 WooCommerce 状态仪表板小部件中重命名单词

问题描述

我想在 WooCommerce 状态仪表板小部件中重命名单词(见屏幕截图)。

我使用以下代码,它适用于小部件的标题,但不适用于标记的单词。这可能是什么原因?

add_filter(  'gettext',  'change_add_to_cart_message22', 10, 3 );
function change_add_to_cart_message22( $translated, $text, $domain  ) {
    global $pagenow;
    if( $text === 'on-hold' && $domain === 'woocommerce' && is_admin() && $pagenow === 'index.php' ){
        $translated = __( 'test', $domain );
    }
    return $translated;
}

在此处输入图像描述


我也试过这个,但结果并不好。它不仅会覆盖文本,还会覆盖用于链接的 HTML。

jQuery( document ).ready(function() {
    jQuery("#woocommerce_dashboard_status .wc_status_list li.processing-orders").text(function () {
       return jQuery(this).text().replace("awaiting", "Websites:"); 
    });
});

在此处输入图像描述

有什么建议吗?

标签: jquerywordpresswoocommercewidgetbackend

解决方案


我宁愿避免的解决方案,但这将是最快/最简单的解决方案之一。

使用 jQuery。确保要替换的文本完全匹配。例如,如果这已经被翻译了,你也应该像这样应用它

所以你得到:

// Prints scripts or data before the default footer scripts.
// This hook is for admin only and can’t be used to add anything on the front end.
function action_admin_footer() {
    ?>
    <script>
        jQuery(document).ready(function($) {
            $( 'ul.wc_status_list .processing-orders a' ).html( function( index, text ) {
                return text.replace( 'awaiting processing', 'first new text' );
            });
            
            $( 'ul.wc_status_list .on-hold-orders a' ).html( function( index, text ) {
                return text.replace( 'on-hold', 'second new text' );
            });
        });
    </script>
    <?php
}
add_action( 'admin_footer', 'action_admin_footer' );

推荐阅读