Php 如何为WooCommerce创建简单的离线支付网关?
下午好,我想在我的商店里制作一份货到付款(COD)的简单副本,并将其重命名为货到付款优惠券。 如何实施 向上 我尝试过此代码,但在WC设置页面上显示错误500:Php 如何为WooCommerce创建简单的离线支付网关?,php,wordpress,woocommerce,hook-woocommerce,Php,Wordpress,Woocommerce,Hook Woocommerce,下午好,我想在我的商店里制作一份货到付款(COD)的简单副本,并将其重命名为货到付款优惠券。 如何实施 向上 我尝试过此代码,但在WC设置页面上显示错误500: <?php /** * Plugin Name: My New WooCommerce Gateway * Plugin URI: * Description: WooCommerce gateway to .... * Author: ..... * Version: 1.0 * Author URI: https:
<?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: 管理>>商业>>设置>>结帐>>货到付款 选中启用COD的选项 请参阅下面的链接以创建脱机支付网关
在WooCommerce插件中,您可以从管理部分启用支付网关COD: 管理>>商业>>设置>>结帐>>货到付款 选中启用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运行时,WooCommerce\u支付\u网关才存在
然后在另一个名为class wc gateway.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\u Gateway\u 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运行时,WooCommerce\u支付\u网关才存在
然后在另一个名为class wc gateway.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\u Gateway\u COD
类,以便您可以从中继承方法,并且只覆盖与事物命名有关的方法。谢谢),但问题是,我需要COD和新的支付方法,它们与COD相同,但有另一个名称(交付时的优惠券)。换句话说,我需要2种离线支付方式。谢谢)但问题是,我需要COD和新的支付方式,这两种支付方式与COD相同,但有另一个名称(交付时优惠券)。换句话说,我需要2种离线支付方式。添加到问题中,在WooCommerce设置页面中显示错误500。类WC\u Gateway\u my\u Gateway
不存在。添加到问题中,在WooCommerce设置页面中显示错误500。类WC\u Gateway\u my\u Gateway
不存在。