Replace a specific word for BACS payment method in Woocommerce order edit pages

对着背影说爱祢 提交于 2019-12-06 15:31:56

here you go

First let add the Change Text function :

function change_text($translated_text, $text, $domain)
    {

        switch ($translated_text) {

            case 'Paid on %1$s @ %2$s':

                $translated_text = __('Placed on %1$s @ %2$s', 'woocommerce');
                break;

        }

        return $translated_text;
    }

The Condition:

Now let's create our condition by getting all order id with payment method wire transfer and if the current post id match our order id then we can call the changing text function as following:

add_action('admin_head', 'current_screen');
function current_screen()
{
    global $post;

    if (empty($post)) {
        return;
    } else {
        $postid = $post->ID;
    }
    $args = array(
        'payment_method' => 'bacs',
        'return' => 'ids',
    );

    $ordersid = wc_get_orders($args);

    if (!empty($postid) && in_array($postid, $ordersid)) {
        add_filter('gettext', 'change_text', 20, 3);
    }
}

This unique lightweight hooked function will replace "Paid" by "Placed" for BACS payment method:

add_filter( 'gettext', 'change_order_edit_text', 20, 3 );
function change_order_edit_text( $translated, $text, $domain ) {
    global $pagenow;

    // Only active on order edit pages
    if( ! is_admin() || $pagenow != 'post.php' || get_post_type($_GET['post']) != 'shop_order' )
        return $translated; // Exit

    // Get the payment method used for the current order
    $payment_method = get_post_meta( $_GET['post'], '_payment_method', true );

    // Replacing the word "Paid" for BACS payment method only
    if ( $translated == 'Paid on %1$s @ %2$s' && isset($payment_method) && $payment_method == 'bacs' )
        $translated = __('Placed on %1$s @ %2$s', 'woocommerce');

    return $translated;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!