【问题标题】:WordPress Plugin WooCommerce, Custom Payment Gateway Settings Not SavingWordPress 插件 WooCommerce,自定义支付网关设置不保存
【发布时间】:2014-03-31 20:41:09
【问题描述】:

我正在为 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。

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    你必须在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' );
    

    编辑:

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

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

    编辑 2:

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

    【讨论】:

    • 嗨,为什么我们需要调用“woocommerce_update_options_payment_gateways_”,因为这也没有在整个类中定义并指向 $this。
    【解决方案2】:

    实际的问题是你的this-&gt;id="example_gateway"id 总是区分大小写,应该没有空格和小写。

    示例:

    function __construct() { 
        $this->id = "bitcoin_woocommerce_integration_gateway"; 
        $this->method_title =( "Bitcoin with BWCIME", 'bitcoin_woocommerce_integration_gateway' );
        $this->title = __( "Bitcoin with BWCIME", 'bitcoin_woocommerce_integration_gateway' );
        $this->method_description = "More later";
    
        //further as same as your code..... 
    
    }
    

    【讨论】:

    • 您应该编辑您的问题以将代码与您的解释分开。首先解释问题出在哪里,然后展示一个更好的例子或使用代码修复。
    【解决方案3】:

    我不完全确定我是如何修复它的,但我相信它与添加函数 admin_options() 有关。

     public function admin_options() {
     ?>
                <h3><?php _e('Bitcoin Payment', 'woothemes'); ?></h3>
                <p><?php _e('Message!.', 'woothemes'); ?></p>
                <table class="form-table">
                <?php
                    // Generate the HTML For the settings form.
                    $this->generate_settings_html();
                ?>
                </table>
                <?php
    } 
    

    编辑:我不确定是什么原因造成的,但您必须通过与数据库交互来清除插件的设置。最简单的方法是更改​​插件的 ID。我实际上不确定设置在数据库中的存储位置。

    【讨论】:

      猜你喜欢
      • 2023-03-03
      • 2014-03-10
      • 2019-01-05
      • 2017-03-02
      • 2017-08-05
      • 2014-06-06
      • 2021-04-24
      • 2022-08-10
      • 2016-08-20
      相关资源
      最近更新 更多