在 WooCommerce 中更改特定的支付网关标题 [英] Change specific payment gateway title in WooCommerce

查看:52
本文介绍了在 WooCommerce 中更改特定的支付网关标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要更改 Woocommerce 支付网关名称,而不是前端显示的名称(这可以在设置中轻松实现),而是 Woo 使用的内部标题.

I need to change the Woocommerce payment gateway names, not the ones hat are displayed on the frontend(this can easily be achieved in the settings) but the inernatl titles Woo is using.

例如在 class-wc-gateway-cheque.php 中我发现了这个

In class-wc-gateway-cheque.php for example I found this

        $this->id  = 'cheque';

但是简单地更改那里的名称是行不通的.如何更改 Woocommerce 在内部为此付款方式使用的名称?

but simply changing the name there did not work. How can I change the name Woocommerce is using internally for this payment method?

推荐答案

所以你可以做的是从 WC_Gateway_Cheque Class 到插件文件,如下所述:

So what you can do instead is to copy the source code from WC_Gateway_Cheque Class to a plugin file as explained below:

要基于现有的 WooCommerce 付款方式作为 cheque 制作自定义网关,建议 从插件中的WC_Gateway_Cheque Class复制源代码(改编您需要的代码).

To make a custom gateway based on an existing WooCommerce payment method as cheque, It's recommended to copy the source code from WC_Gateway_Cheque Class in a plugin (adapting the code for your needs).

您可以将代码复制到您将命名的 php 文件中,例如 wc-invoice-payments.php.

You can copy the code to a php file that you will name for example wc-invoice-payments.php.

要复制的代码:

<?php
/**
 * Plugin Name: WooCommerce Invoice Gateway
 * Plugin URI:
 * Description: Clones the "Cheque" gateway to create another custom payment method.
 * Author: Your name
 * Author URI: http://www.something.tld/
 * Version: 1.0.0
 * Text Domain: wc-invoice-gateway
 * Domain Path: /i18n/languages/
 *
 * Copyright: (c) 2016-2018
 *
 * License: GNU General Public License v3.0
 * License URI: http://www.gnu.org/licenses/gpl-3.0.html
 *
 * @package   wc-invoice-gateway
 * @author    Your name
 * @category  Admin
 * @copyright Copyright (c) 2020
 * @license   http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
 *
 * This "Invoice" gateway forks the WooCommerce core "Cheque" payment gateway to create another custom payment method.
 */
defined( 'ABSPATH' ) or exit;
// Make sure WooCommerce is active
if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
    return;
}
/**
 * Add the gateway to WC Available Gateways
 *
 * @since 1.0.0
 * @param array $gateways all available WC gateways
 * @return array $gateways all WC gateways + Custom gateway
 */
function wc_invoice_add_to_gateways( $gateways ) {
    $gateways[] = 'WC_Invoice_Gateway';
    return $gateways;
}
add_filter( 'woocommerce_payment_gateways', 'wc_invoice_add_to_gateways' );
/**
 * Adds plugin page links
 *
 * @since 1.0.0
 * @param array $links all plugin links
 * @return array $links all plugin links + our custom links (i.e., "Settings")
 */
function wc_gateway_invoice_plugin_links( $links ) {
    $plugin_links = array(
        '<a href="' . admin_url( 'admin.php?page=wc-settings&tab=checkout&section=invoice' ) . '">' . __( 'Configure', 'wc-invoice-gateway' ) . '</a>'
    );
    return array_merge( $plugin_links, $links );
}
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'wc_gateway_invoice_plugin_links' );
/**
 * Invoice Payment Gateway
 *
 * Provides an Custom Payment Gateway; mainly for testing purposes.
 * We load it later to ensure WC is loaded first since we're extending it.
 *
 * @class       WC_Invoice_Gateway
 * @extends     WC_Payment_Gateway
 * @version     1.0.0
 */
add_action( 'plugins_loaded', 'wc_invoice_gateway_init', 11 );
function wc_invoice_gateway_init() {
    class WC_Invoice_Gateway extends WC_Payment_Gateway {
        /**
         * Constructor for the gateway.
         */
        public function __construct() {
            $this->id                 = 'invoice';
            $this->domain             = 'wc-invoice-gateway';
            $this->method_title       = _x( 'Invoice payments', 'Invoice payment method', $this->domain );
            $this->icon               = apply_filters( 'woocommerce_invoice_icon', '' );
            $this->has_fields         = false;
            $this->method_description = __( 'Take payments in person via Invoice. This offline gateway can also be useful to test purchases.', $this->domain );

            // Load the settings.
            $this->init_form_fields();
            $this->init_settings();

            // Define user set variables.
            $this->title        = $this->get_option( 'title' );
            $this->description  = $this->get_option( 'description' );
            $this->instructions = $this->get_option( 'instructions' );

            // Actions.
            add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
            add_action( 'woocommerce_thankyou_invoice', array( $this, 'thankyou_page' ) );

            // Customer Emails.
            add_action( 'woocommerce_email_before_order_table', array( $this, 'email_instructions' ), 10, 3 );
        }
        /**
         * Initialize Gateway Settings Form Fields
         */
        public function init_form_fields() {
            $this->form_fields = array(
                'enabled'      => array(
                    'title'   => __( 'Enable/Disable', $this->domain ),
                    'type'    => 'checkbox',
                    'label'   => __( 'Enable Invoice payments', $this->domain ),
                    'default' => 'no',
                ),
                'title'        => array(
                    'title'       => __( 'Title', $this->domain ),
                    'type'        => 'text',
                    'description' => __( 'This controls the title which the user sees during checkout.', $this->domain ),
                    'default'     => _x( 'Invoice', 'Invoice payment method', $this->domain ),
                    'desc_tip'    => true,
                ),
                'description'  => array(
                    'title'       => __( 'Description', $this->domain ),
                    'type'        => 'textarea',
                    'description' => __( 'Payment method description that the customer will see on your checkout.', $this->domain ),
                    'default'     => __( 'Receive an invoice...', $this->domain ),
                    'desc_tip'    => true,
                ),
                'instructions' => array(
                    'title'       => __( 'Instructions', $this->domain ),
                    'type'        => 'textarea',
                    'description' => __( 'Instructions that will be added to the thank you page and emails.', $this->domain ),
                    'default'     => '',
                    'desc_tip'    => true,
                ),
            );
        }

        /**
         * Output for the order received page.
         */
        public function thankyou_page() {
            if ( $this->instructions ) {
                echo wp_kses_post( wpautop( wptexturize( $this->instructions ) ) );
            }
        }

        /**
         * Add content to the WC emails.
         *
         * @access public
         * @param WC_Order $order Order object.
         * @param bool     $sent_to_admin Sent to admin.
         * @param bool     $plain_text Email format: plain text or HTML.
         */
        public function email_instructions( $order, $sent_to_admin, $plain_text = false ) {
            if ( $this->instructions && ! $sent_to_admin && 'invoice' === $order->get_payment_method() && $order->has_status( 'on-hold' ) ) {
                echo wp_kses_post( wpautop( wptexturize( $this->instructions ) ) . PHP_EOL );
            }
        }

        /**
         * Process the payment and return the result.
         *
         * @param int $order_id Order ID.
         * @return array
         */
        public function process_payment( $order_id ) {

            $order = wc_get_order( $order_id );

            if ( $order->get_total() > 0 ) {
                // Mark as on-hold (we're awaiting the invoice).
                $order->update_status( apply_filters( 'woocommerce_invoice_process_payment_order_status', 'on-hold', $order ), _x( 'Awaiting Invoice payment', 'Invoice payment method', $this->domain ) );
            } else {
                $order->payment_complete();
            }

            // Remove cart.
            WC()->cart->empty_cart();

            // Return thankyou redirect.
            return array(
                'result'   => 'success',
                'redirect' => $this->get_return_url( $order ),
            );
        }
    } // end \WC_Invoice_Gateway class
}

代码位于活动子主题(或活动主题)的 functions.php 文件中.经测试有效.

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

然后在管理员中启用 WooCommerce Invoice Gateway 插件.
现在在 WooCommerce 设置的付款部分,您可以启用此付款网关.

Then enable WooCommerce Invoice Gateway plugin in admin.
Now in WooCommerce settings, Payments section, you can enable this payment gateway.

您可以取消设置/删除原始支票支付网关,更改第一个功能,例如:

You can unset / remove original Cheque payment gateway changing the 1st function like:

add_filter( 'woocommerce_payment_gateways', 'wc_invoice_add_to_gateways' );
function wc_invoice_add_to_gateways( $gateways ) {
    $gateways[] = 'WC_Invoice_Gateway';

    unset($gateways['WC_Gateway_Cheque']; // Remove Cheque gateway

    return $gateways;
}

它应该按预期工作.

相关:扩展 WooCommerce COD 支付网关插件

初始答案:

由于所有支付网关都扩展了 WC_Payment_Gateway 类,如果您查看get_title() 方法,您将看到可以使用过滤器钩子woocommerce_gateway_title.

As all payment gateways extend WC_Payment_Gateway Class, if you look to get_title() method you will see that you can use the filter hook woocommerce_gateway_title.

所以对于"cheque"付款ID,您将按如下方式使用它:

So for "cheque" payment Id, you will use it as follow:

add_filter( 'woocommerce_gateway_title', 'change_cheque_payment_gateway_title', 100, 2 );
function change_cheque_payment_gateway_title( $title, $payment_id ){
    if( $payment_id === 'cheque' ) {
        $title = __("Something else", "woocommerce");
    }
    return $title;
}

代码位于活动子主题(或活动主题)的 functions.php 文件中.经测试有效.

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

这篇关于在 WooCommerce 中更改特定的支付网关标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆