我正在为 woocommerce 创建自定义支付网关。虽然它显示在管理员上,但在结帐页面上不可用。请参阅下面的代码
<?php
/*
Plugin Name: Custom Payment Gateway
Description: Custom payment gateway for WooCommerce.
Version: 1.0
Author: Your Name
*/
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly.
}
// Initialize the gateway
add_action('plugins_loaded', 'init_custom_gateway');
function init_custom_gateway() {
if (!class_exists('WC_Payment_Gateway')) {
return;
}
class WC_Custom_Gateway extends WC_Payment_Gateway {
public function __construct() {
$this->id = 'custom_gateway';
$this->method_title = __('Custom Gateway', 'woocommerce');
$this->method_description = __('Custom payment gateway for WooCommerce', 'woocommerce');
$this->title = $this->get_option('title');
$this->has_fields = true;
// Load settings
$this->init_form_fields();
$this->init_settings();
// Define user set variables
$this->title = $this->get_option('title');
// Actions
add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
add_action('woocommerce_receipt_' . $this->id, array($this, 'receipt_page'));
}
// Gateway Fields
public function init_form_fields() {
$this->form_fields = array(
'enabled' => array(
'title' => __('Enable/Disable', 'woocommerce'),
'type' => 'checkbox',
'label' => __('Enable Custom Gateway', 'woocommerce'),
'default' => 'yes',
),
'title' => array(
'title' => __('Title', 'woocommerce'),
'type' => 'text',
'description' => __('Payment method title that the customer will see on your website.', 'woocommerce'),
'default' => __('Custom Gateway', 'woocommerce'),
'desc_tip' => true,
),
);
}
// Payment form on checkout page
public function payment_fields() {
echo '<p>' . __('Custom payment information', 'woocommerce') . '</p>';
}
// Process the payment and return the result
public function process_payment($order_id) {
$order = wc_get_order($order_id);
// Mark as on-hold (we're awaiting the cheque)
$order->update_status('on-hold', __( 'Awaiting cheque payment', 'woocommerce' ));
// Mark as processing (payment received)
// $order->update_status('processing');
// Reduce stock levels
// $order->reduce_order_stock();
wc_reduce_stock_levels( $order_id );
// Remove cart
WC()->cart->empty_cart();
// Return a success redirect
return array(
'result' => 'success',
'redirect' => $this->get_return_url($order),
);
}
// Receipt page
public function receipt_page($order_id) {
echo '<p>' . __('Thank you for your order.', 'woocommerce') . '</p>';
}
}
// Register the gateway
function add_custom_gateway($methods) {
$methods[] = 'WC_Custom_Gateway';
return $methods;
}
add_filter('woocommerce_payment_gateways', 'add_custom_gateway');
}
我已在 Woocommerce 设置下激活并启用了该插件,但仍然无法工作
我的结帐页面看起来仍然像那样
我可能做错了什么?
你找到解决办法了吗?我也有同样的问题。