Replace a specific word for BACS payment method in

2019-05-30 11:12发布

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

2条回答
劳资没心,怎么记你
2楼-- · 2019-05-30 11:42

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.

查看更多
贼婆χ
3楼-- · 2019-05-30 11:49

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);
    }
}
查看更多
登录 后发表回答