WordPress WooCommerce支付网关插件 管理员测试

这是一个用于WooCommerce的支付网关插件,用于处理测试或无付款交易。它允许管理员或具有特定IP地址的用户使用此支付网关,以便在非生产环境中进行测试或用于无付款商店。插件提供了一个名为WC_Gateway_sb_test的类,并使用钩子函数将其添加到WooCommerce的支付网关列表中。该类实现了必要的方法,包括构造函数、init_form_fields()、admin_options()和process_payment(),并且允许管理员通过插件设置启用或禁用插件以及其他相关设置。

<?php

/**
 * Plugin Name: Test Payment Module for Woocommerce
 * Plugin URI:  http://www.tortoise-it.co.uk
 * Description: A payment gateway plugin for Woocommerce to handle test or paymentless transactions. Shows for admin only by default or everyone in WP_DEBUG or using a gateway setting
 * Author:      Sean Barton (Tortoise IT)
 * Author URI:  http://www.tortoise-it.co.uk
 * Version:     1.5
 */

function sb_wc_test_init() {
  if (!class_exists('WC_Payment_Gateway')) {
    return;
  }
  
  class WC_Gateway_sb_test extends WC_Payment_Gateway {
  
    public function __construct() {
      $this->id = 'sb_test';
      $this->has_fields = false;
      $this->method_title = __( 'Test/Paymentless', 'woocommerce' );
      $this->init_form_fields();
      $this->init_settings();
      $this->title = 'Test/Paymentless gateway';
  
      add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
    }
    
    function init_form_fields() {
      $this->form_fields = array(
        'enabled' => array(
          'title' => __( 'Enable/Disable', 'woocommerce' ),
          'type' => 'checkbox',
          'label' => __( 'Enable test gateway', 'woocommerce' ),
          'default' => 'yes'
        ),
        'enabled_visitors' => array(
          'title' => __( 'Enable for visitors', 'woocommerce' ),
          'type' => 'checkbox',
          'label' => __( 'Allow non admins to use this gateway (for testing or for paymentless stores)', 'woocommerce' ),
          'default' => 'no'
        ),
        'enabled_ip' => array(
          'title' => __( 'Enable for specific IP addresses', 'woocommerce' ),
          'type' => 'textarea',
          'label' => __( 'In the following field enter IP addresses (one per line) to enable this gateway for specific IPs', 'woocommerce' ),
          'default' => ''
        )
      );
    }
      
    
    public function admin_options() {
      echo '  <h3>Test/Paymentless gateway</h3>
        <table class="form-table">';
        
      $this->generate_settings_html();
      
      echo '  </table>';
    }
  
    public function process_payment( $order_id ) {
      global $woocommerce;
      
      $order = new WC_Order( $order_id );
      $order->payment_complete();
      $order->reduce_order_stock();
      $woocommerce->cart->empty_cart();
  
      return array(
        'result' => 'success',
        //'redirect' => add_query_arg('key', $order->order_key, add_query_arg('order', $order->id, get_permalink(woocommerce_get_page_id('thanks')))),
        'redirect' => $order->get_checkout_order_received_url()
      );
    }
  
  }  

  function add_sb_test_gateway( $methods ) {
    
    $show_visitors = $show_ip = false;
    
    if ($settings = get_option('woocommerce_sb_test_settings')) {
      if (isset($settings['enabled_visitors']) && $settings['enabled_visitors'] == 'yes') {
        $show_visitors = true;
      }
      if (isset($settings['enabled_ip'])) {
        if ($ips = explode("\n", $settings['enabled_ip'])) {
          foreach ($ips as $ip) {
            $ip = trim($ip);
            if ($_SERVER['REMOTE_ADDR'] == $ip) {
              $show_ip = true;
              break;
            }
          }
        }
      }
    }
    
    if (current_user_can('administrator') || WP_DEBUG || $show_visitors || $show_ip) {
      $methods[] = 'WC_Gateway_sb_test';
    }
    
    return $methods;
  }
  
  add_filter('woocommerce_payment_gateways', 'add_sb_test_gateway' );
  
}

add_filter('plugins_loaded', 'sb_wc_test_init' );

?>
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

给TA打赏
共{{data.count}}人
人已打赏
html5php前端后端

Wordpress添加可以重复使用的自定义metabox字段

2023-5-9 22:24:11

php

WordPress B2主题输出投稿里的存储在数据库的自定义字段

2023-6-7 14:13:44

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索