这几天老板有个需求:要在网站上提供一个新的支付方式给客户支付,于是乎研究了一下woocommerce怎么新增网关,捣鼓了半天终于让我搞出来了,分享出来给后来者。我会尽量在代码中注释,让代码清楚一些。(注:这个是线下支付方式的网关,还有其他诸如线上支付,银行转账看看文档也差不多能整出来的)
//将你自定义的网关作为插件加载进来
add_action( 'plugins_loaded', 'init_your_gateway_class' );
function init_your_gateway_class() {
//定义一个网关类继承 WC_Payment_Gateway 类
class WC_Gateway_West_Union_Gateway extends WC_Payment_Gateway {
/**
* Constructor for the gateway.
*/
public function __construct() {
//在构造函数中定义好网关的ID(必要的) 也一定要是唯一的。
//icon图标、
//has_fields是在线上支付是用到的,线下支付设置为false就行
//method_title : 后台显示的标题
//method_description : 后台显示的说明
$this->id = 'west';
$this->icon = apply_filters( 'woocommerce_cheque_icon', '' );
$this->has_fields = false;
$this->method_title = __( '西联付款', 'woocommerce' );
$this->method_description = __( '西联付款', 'woocommerce' );
// Load the settings. 初始化设置字段并获取设置
$this->init_form_fields();
$this->init_settings();
// Define user set variables. 设置变量
$this->title = $this->get_option( 'title' );
$this->description = $this->get_option( 'description' );
$this->instructions = $this->get_option( 'instructions' );
// Actions. 保存设置钩子
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
//支付完成后的跳转钩子
add_action( 'woocommerce_thankyou_west', array( $this, 'thankyou_page' ) );
// Customer Emails. email钩子
add_action( 'woocommerce_email_before_order_table', array( $this, 'email_instructions' ), 10, 3 );
}
/**
* Initialise Gateway Settings Form Fields. 初始化表单字段
*/
public function init_form_fields() {
//我们定义了是否启用字段、标题、说明、发送给客户的说明文字
$this->form_fields = array(
'enabled' => array(
'title' => __( 'Enable/Disable', 'woocommerce' ),
'type' => 'checkbox',
'label' => __( 'Enable Western Union payments', 'woocommerce' ),
'default' => 'no',
),
'title' => array(
'title' => __( 'Title', 'woocommerce' ),
'type' => 'text',
'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
'default' => __( 'Western Union', 'woocommerce' ),
'desc_tip' => true,
),
'description' => array(
'title' => __( 'Description', 'woocommerce' ),
'type' => 'textarea',
'description' => __( 'Payment method description that the customer will see on your checkout.', 'woocommerce' ),
'default' => __( '', 'woocommerce' ),
'desc_tip' => true,
),
'instructions' => array(
'title' => __( 'Instructions', 'woocommerce' ),
'type' => 'textarea',
'description' => __( 'Instructions that will be added to the thank you page and emails.', 'woocommerce' ),
'default' => '',
'desc_tip' => true,
),
);
}
/**
* Output for the order received page. 输出内容到订单接收页
*/
public function thankyou_page() {
if ( $this->instructions ) {
echo wp_kses_post( wpautop( wptexturize( $this->instructions ) ) );
}
}
/**
* Add content to the WC emails. 添加内容到email中
*
* @access public
* @param WC_Order $order Order object.
* @param bool $sent_to_admin Sent to admin.
* @param bool $plain_text Email format: plain text or HTML.
*/
public function email_instructions( $order, $sent_to_admin, $plain_text = false ) {
if ( $this->instructions && ! $sent_to_admin && 'west' === $order->get_payment_method() && $order->has_status( 'on-hold' ) ) {
echo wp_kses_post( wpautop( wptexturize( $this->instructions ) ) . PHP_EOL );
}
}
/**
* Process the payment and return the result.
*
* @param int $order_id Order ID.
* @return array
*/
public function process_payment( $order_id ) {
$order = wc_get_order( $order_id );
if ( $order->get_total() > 0 ) {
// Mark as on-hold (we're awaiting the west).
$order->update_status( apply_filters( 'woocommerce_west_process_payment_order_status', 'on-hold', $order ), __( 'Awaiting WesternUnion payment method', 'woocommerce' ) );
} else {
$order->payment_complete();
}
// Remove cart.
WC()->cart->empty_cart();
// Return thankyou redirect.
return array(
'result' => 'success',
'redirect' => $this->get_return_url( $order ),
);
}
}
}
function add_your_gateway_class( $methods ) {
$methods[] = 'WC_Gateway_West_Union_Gateway';
return $methods;
}
//将我们定义好的网关加入到woocommerce的网关列表中
add_filter( 'woocommerce_payment_gateways', 'add_your_gateway_class' );
要自定义woocommerce主要还是多读读文档吧,毕竟是人家的东西,规矩得按人家的来。文档读多了就懂了。
参考链接:Payment Gateway API