在 WooCommerce 中更改特定的支付网关标题

Posted

技术标签:

【中文标题】在 WooCommerce 中更改特定的支付网关标题【英文标题】:Change specific payment gateway title in WooCommerce 【发布时间】:2021-01-07 05:18:30 【问题描述】:

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

以 class-wc-gateway-cheque.php 为例,我发现了这个

        $this->id  = 'cheque';

但只是在那里更改名称不起作用。如何更改 Woocommerce 内部为此付款方式使用的名称?

【问题讨论】:

【参考方案1】:

因此,您可以将源代码从WC_Gateway_Cheque Class 复制到插件文件,如下所述:

要基于现有的WooCommerce 支付方式cheque 制作自定义网关,建议在插件中使用copy the source code from WC_Gateway_Cheque Class(根据您的需要调整代码)

您可以将代码复制到您将命名的 php 文件中,例如 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 文件中。经过测试并且可以工作。

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

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

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;

它应该按预期工作。

相关:Extending WooCommerce COD payment gateway in a plugin


初步答案:

由于所有支付网关都扩展了 WC_Payment_Gateway 类,如果您look to get_title() method,您将看到您可以使用过滤器挂钩woocommerce_gateway_title

所以对于"cheque"支付ID,你将使用它如下:

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 文件中。经过测试并且可以工作。

【讨论】:

虽然只是在前面改变了它......而不是“内部”。问题是我正在使用支票网关进行发票支付,并且我正在使用通过 Woocommerce API 连接的会计工具。当使用支票下订单时,此工具总是会收到“支票”信息,但我需要它是“发票”。

以上是关于在 WooCommerce 中更改特定的支付网关标题的主要内容,如果未能解决你的问题,请参考以下文章

更改 Woocommerce 中默认选择的支付网关

在 Woocommerce 中为特定支付网关添加自定义费用

如何在 woocommerce 上制作特定的支付网关以免费送货

如何通过 WooCommerce 中管理员端的更改订单设置支付网关

根据 Woocommerce 中的用户角色更改 COD 支付网关的默认订单状态

支付网关的 WooCommerce 订单状态更改