如何为WooCommerce创建简单的离线支付网关?

时间:2017-04-22 09:09:58

标签: 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://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');

Source of code

2 个答案:

答案 0 :(得分:1)

在WooCommerce插件中,您可以从管理部分启用支付网关COD:

管理员&gt;&gt; WooCommerce&gt;&gt;设置&gt;&gt;结帐&gt;&gt;货到付款。

选中启用COD的选项。

请参阅以下链接以创建离线支付网关。

How to Create A WooCommerce Payment gateway

答案 1 :(得分:1)

在您的主插件文件中,您可以包含该类并同时过滤网关:

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类,以便您可以从中继承方法,并且只覆盖与命名有关的方法。