php - WordPress 插件 WooCommerce,自定义支付网关设置未保存

标签 php wordpress woocommerce

我正在为 WordPress 插件 WooCommerce 开发自定义支付网关。我似乎无法保存支付网关的设置。当我在字段中输入信息然后单击保存时,页面刷新,所有字段均为空白。我做错了什么?

这是我的代码。

<?php
/**
 * Plugin Name: Bitcoin WooCommerce Integration Made Easy
 * Description: A Bitcoin processing plugin that integrates into WooCommerce made specifically for Bitcoin Publish.
 * Version: 0.01
 * Author: Cammy_the_block
 */

add_action( 'plugins_loaded', 'init_your_gateway_class' );

function init_your_gateway_class() {
    class WC_Gateway_Your_Gateway extends WC_Payment_Gateway {
        function __construct() {
            $this->id = "Bitcoin WooCommerce Integration Gateway";
            $this->method_title = "Bitcoin with BWCIME";
            $this->method_description = "More later";

            $this->init_form_fields();
            $this->init_settings();

            if ( version_compare( WOOCOMMERCE_VERSION, '2.0.0', '>=' ) ) {
                    add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( &$this, 'process_admin_options' ) );
            } 
            else {
                    add_action( 'woocommerce_update_options_payment_gateways', array( &$this, 'process_admin_options' ) );
            }
        }
        function init_form_fields(){
            $this->form_fields = array(
                    'enabled' => array(
                            'title' => __( 'Enable/Disable', 'woocommerce' ),
                            'type' => 'checkbox',
                            'label' => __( 'Enable Cheque Payment', 'woocommerce' ),
                            'default' => 'yes'
                    ),
                    'title' => array(
                            'title' => __( 'Title', 'woocommerce' ),
                            'type' => 'text',
                            'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
                            'default' => __( 'Cheque Payment', 'woocommerce' ),
                            'desc_tip' => true,
                    ),
                    'description' => array(
                            'title' => __( 'Customer Message', 'woocommerce' ),
                            'type' => 'textarea',
                            'default' => ''
                    )
            );
        }
    }
    function process_payment( $order_id ) {
        global $woocommerce;
        $order = new WC_Order( $order_id );
        $productArray = array();
        $x = 0;
        foreach( $order->get_items() as $item_id => $item ) {
            $productArray[x] = $order->get_product_from_item( $item );
            $x++;
        }

        // Mark as on-hold (we're awaiting the cheque)
        $order->update_status('on-hold', 
        __( 'Awaiting cheque payment. there are ' + $productArray.length + 'items', 'woocommerce' )
        );


        // Remove cart
        $woocommerce->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_Your_Gateway'; 
    return $methods;
}

add_filter( 'woocommerce_payment_gateways', 'add_your_gateway_class' );
 ?>

编辑:

添加过滤器代码运行 add_your_gateway_class,这反过来导致它运行 WC_Gateway_Your_Gateway。

最佳答案

您必须在 init_settings() 之后的构造函数中调用它们;

    $this->init_settings();
    // Define user set variables
    $this->access_key = $this->get_option( 'access_key' );
    $this->title = $this->get_option( 'title' );
    $this->description = $this->get_option( 'description' );

编辑:

你还需要另一个 Action / Hook ,就在构造函数的末尾,不需要创建你想出的新函数:

            add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );

编辑 2:

抱歉你已经有了它,我的错 :p,我真的不知道那里发生了什么,很高兴它解决了

关于php - WordPress 插件 WooCommerce,自定义支付网关设置未保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22085110/

相关文章:

javascript - 在选择标签中仅显示一次选项

php - Bootstrap 3.x 的粘性页脚不起作用

linux - htaccess 将 www 重定向到非 www 不工作

php - Wordpress:在显示链接之前检查是否有以前的帖子

wordpress - 在钩子(Hook) woocommerce_checkout_order_processed 中获取订单详细信息

php - 排序具有指定键值的数组 PHP

php - 只是为了好玩 - 开始解决一个棘手的 PHP 逻辑问题

php - 为 WooCommerce 中的特定订单状态启用 "cancel"订单操作按钮

javascript - 防止在加载页面上滚动

php - 在 Woocommerce "New account"电子邮件通知模板中获取用户电子邮件