在你的主要插件文件可以包含类,并在同一时间过滤网关:
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