如何为WooCommerce创建简单的离线支付网关?
问题描述:
下午好,我想在我的商店中制作货到付款(COD)付款方式的简单副本,并将其重命名为Coupon on Delivery。 如何实现它?如何为WooCommerce创建简单的离线支付网关?
最多 我曾尝试这个代码,但它表明WC设置页面上的错误500:
<?php
/**
* Plugin Name: My New WooCommerce Gateway
* Plugin URI:
* Description: WooCommerce gateway to ....
* Author: .....
* Version: 1.0
* Author URI: https://example.org/
* Text Domain: woocommerce-my-gateway
* Domain Path: /languages/
*/
add_action('plugins_loaded', 'init_my_gateway_class');
function init_my_gateway_class() {
if (!class_exists('WooCommerce')) return;
class WC_Gateway_COD_Renamed extends WC_Payment_Gateway {
}
}
function add_my_gateway_class($methods) {
$methods[] = 'WC_Gateway_my_gateway';
return $methods;
}
add_filter('woocommerce_payment_gateways', 'add_my_gateway_class');
function my_load_textdomain(){
load_plugin_textdomain('woocommerce-my-gateway', false, dirname(plugin_dir_path(__FILE__) . '/languages/'));
}
add_action('plugins_loaded', 'my_load_textdomain');
答
在WooCommerce插件,您可以从管理部分启用支付网关COD:
管理>> WooCommerce >>设置>>结帐>>货到付款。
检查启用COD的选项。
请参考下面的链接创建离线支付网关。
答
在你的主要插件文件可以包含类,并在同一时间过滤网关:
function add_my_gateway_class($methods) {
include('class-wc-gateway-cod-renamed.php');
$methods[] = 'WC_Gateway_COD_Renamed';
return $methods;
}
add_filter('woocommerce_payment_gateways', 'add_my_gateway_class');
有没有真正的需要检查WooCommerce是活跃的这种方式woocommerce_payment_gateways
只WooCommerce正在运行时存在。
然后在另一个名为文件class-wc-gateway-cod-renamed.php
你可以定义你的类:
class WC_Gateway_COD_Renamed extends WC_Gateway_COD {
/**
* Setup general properties for the gateway.
*/
protected function setup_properties() {
$this->id = 'coupon-on-delivery';
$this->icon = apply_filters('woocommerce_coupon-on-deliver_icon', '');
$this->method_title = __('Coupon on delivery', 'your-plugin');
$this->method_description = __('Have your customers pay with a coupon upon delivery.', 'your-plugin');
$this->has_fields = false;
}
/**
* Initialise Gateway Settings Form Fields.
*/
public function init_form_fields() {
$shipping_methods = array();
foreach (WC()->shipping()->load_shipping_methods() as $method) {
$shipping_methods[ $method->id ] = $method->get_method_title();
}
$this->form_fields = array(
'enabled' => array(
'title' => __('Enable/Disable', 'your-plugin'),
'label' => __('Enable coupon on delivery', 'your-plugin'),
'type' => 'checkbox',
'description' => '',
'default' => 'no',
),
'title' => array(
'title' => __('Title', 'your-plugin'),
'type' => 'text',
'description' => __('Payment method description that the customer will see on your checkout.', 'your-plugin'),
'default' => __('coupon on delivery', 'your-plugin'),
'desc_tip' => true,
),
'description' => array(
'title' => __('Description', 'your-plugin'),
'type' => 'textarea',
'description' => __('Payment method description that the customer will see on your website.', 'your-plugin'),
'default' => __('Pay with coupon upon delivery.', 'your-plugin'),
'desc_tip' => true,
),
'instructions' => array(
'title' => __('Instructions', 'your-plugin'),
'type' => 'textarea',
'description' => __('Instructions that will be added to the thank you page.', 'your-plugin'),
'default' => __('Pay with coupon upon delivery.', 'your-plugin'),
'desc_tip' => true,
),
'enable_for_methods' => array(
'title' => __('Enable for shipping methods', 'your-plugin'),
'type' => 'multiselect',
'class' => 'wc-enhanced-select',
'css' => 'width: 400px;',
'default' => '',
'description' => __('If coupon upon delivery is only available for certain methods, set it up here. Leave blank to enable for all methods.', 'your-plugin'),
'options' => $shipping_methods,
'desc_tip' => true,
'custom_attributes' => array(
'data-placeholder' => __('Select shipping methods', 'your-plugin'),
),
),
'enable_for_virtual' => array(
'title' => __('Accept for virtual orders', 'your-plugin'),
'label' => __('Accept coupon if the order is virtual', 'your-plugin'),
'type' => 'checkbox',
'default' => 'yes',
),
);
}
}
扩展WC_Gateway_COD
类,这样就可以继承这个方法,只覆盖有事物的命名做的方法。
已添加到问题 – Darklez
它显示WooCommerce设置页面中的错误500。 – Darklez
“WC_Gateway_my_gateway”类不存在。 – helgatheviking