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

问题描述

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

例如,在class-wc-gateway-cheque.PHP中,我发现了这一点

        $this->id  = 'cheque';

,但仅在此处更改名称是无效的。我该如何更改Woocommerce内部使用的付款方式名称

解决方法

因此,您可以做的是将源代码从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','thankyou_page' ) );

            // Customer Emails.
            add_action( 'woocommerce_email_before_order_table','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','default' => 'no',),'title'        => array(
                    'title'       => __( 'Title','type'        => 'text','description' => __( 'This controls the title which the user sees during checkout.','default'     => _x( 'Invoice','desc_tip'    => true,'description'  => array(
                    'title'       => __( 'Description','type'        => 'textarea','description' => __( 'Payment method description that the customer will see on your checkout.','default'     => __( 'Receive an invoice...','instructions' => array(
                    'title'       => __( 'Instructions','description' => __( 'Instructions that will be added to the thank you page and emails.','default'     => '',);
        }

        /**
         * 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',$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发票网关插件。
现在,在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文件中。经过测试,可以正常工作。