muraliniceguy97
muraliniceguy97

Reputation: 381

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

Am new to woocommerce, by using gettext hook am able to replace the text "paid" with "placed" but i want to display this text based on one condition i.e when customer pick wire transfer(bacs) as there was no payment received then only text needs to replace with placed

I've attached an image. enter image description here

Upvotes: 1

Views: 763

Answers (2)

LoicTheAztec
LoicTheAztec

Reputation: 254019

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.

Upvotes: 1

kashalo
kashalo

Reputation: 3562

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);
    }
}

Upvotes: 1

Related Questions