在插件中扩展 WooCommerce COD 支付网关 [英] Extending WooCommerce COD payment gateway in a plugin

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

问题描述

我想了解在 Wordpress 中加载各种类的顺序.Wordpress 有很多插件可用,那么谁会比另一个更早加载.

I would like to understand the sequence various classes are loaded in Wordpress. There are many plugins available in Wordpress, then who will be loaded earlier than another.

考虑我想开发一个插件来使用一些现有的 Woocommerce 类.基本上我的自定义插件会扩展一些 Woocommerce 类(例如:WC_Gateway_COD)

Consider I would like to develop a plugin that will use some existing class of Woocommerce. Basically my custom plugin will extend some class of Woocommerce (for example : WC_Gateway_COD)

我如何确保已经定义了现有类WC_Gateway_COD"&在执行以下语句之前加载?

How I can ensure the existing class ‘WC_Gateway_COD’ is already defined & loaded before it execute the below statement ?

if (class_exists('WC_Gateway_COD')) {
     class WC_my_custom_class extends WC_Gateway_COD {
             …..
             ….
     }
}  

推荐答案

要基于现有的 WooCommerce 付款方式作为 COD 制作自定义网关,建议 将源代码从WC_Gateway_COD Class复制到一个插件(根据您的需要调整代码),例如:

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

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_filter( 'woocommerce_payment_gateways', 'wc_custom_add_to_gateways' );
function wc_custom_add_to_gateways( $gateways ) {
    $gateways[] = 'WC_Gateway_COD2';
    return $gateways;
}

add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'wc_gateway_custom_plugin_links' );
function wc_gateway_custom_plugin_links( $links ) {
    $plugin_links = array(
        '<a href="' . admin_url( 'admin.php?page=wc-settings&tab=checkout&section=cod2' ) . '">' . __( 'Configure', 'payment_cod2' ) . '</a>'
    );
    return array_merge( $plugin_links, $links );
}

add_action( 'plugins_loaded', 'wc_gateway_cod2_init', 11 );
function wc_gateway_cod2_init() {
    class WC_Gateway_COD2 extends WC_Payment_Gateway {

        public $domain; // The text domain (optional)

        /**
         * Constructor for the gateway.
         */
        public function __construct() {
            $this->domain = 'payment_cod2'; // text domain name (for translations)

            // Setup general properties.
            $this->setup_properties();
    
            // Load the settings.
            $this->init_form_fields();
            $this->init_settings();
    
            // Get settings.
            $this->title              = $this->get_option( 'title' );
            $this->description        = $this->get_option( 'description' );
            $this->instructions       = $this->get_option( 'instructions' );
            $this->enable_for_methods = $this->get_option( 'enable_for_methods', array() );
            $this->enable_for_virtual = $this->get_option( 'enable_for_virtual', 'yes' ) === 'yes';
    
            add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
            add_action( 'woocommerce_thankyou_' . $this->id, array( $this, 'thankyou_page' ) );
            add_filter( 'woocommerce_payment_complete_order_status', array( $this, 'change_payment_complete_order_status' ), 10, 3 );
    
            // Customer Emails.
            add_action( 'woocommerce_email_before_order_table', array( $this, 'email_instructions' ), 10, 3 );
        }
    
        /**
         * Setup general properties for the gateway.
         */
        protected function setup_properties() {
            $this->id                 = 'cod2';
            $this->icon               = apply_filters( 'woocommerce_cod2_icon', '' );
            $this->method_title       = __( 'Cash on delivery', 'woocommerce' );
            $this->method_description = __( 'Have your customers pay with cash (or by other means) upon delivery.', 'woocommerce' );
            $this->has_fields         = false;
        }

        // and so on (the rest of the code)…
    }
}

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

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

add_filter( 'woocommerce_payment_gateways', 'wc_custom_add_to_gateways' );
function wc_custom_add_to_gateways( $gateways ) {
    $gateways[] = 'WC_Gateway_COD2';

    unset($gateways['WC_Gateway_COD']; // Remove COD gateway

    return $gateways;
}

这篇关于在插件中扩展 WooCommerce COD 支付网关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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