简体   繁体   English

Woocommerce自定义支付网关回调功能不起作用

[英]Woocommerce Custom payment gateway callback function not working

I have been struggling with this for some time now. 我已经为此苦了一段时间了。 I almost explored all google replies etc... 我几乎浏览了所有Google回复等...

Here is my code: 这是我的代码:

class WC_Gateway_mysolugion extends WC_Payment_Gateway {

public function __construct() {

    ...
    ...

    /* Hook IPN callback logic*/
    add_action( 'woocommerce_api_wc_gateway_mysolugion', array( $this, 'check_callback' ) );

  }

  ....

  function check_callback() {
     // other code
  }

The problem is that the function check_callback never get called when controll returns back to the site from payment gateway site. 问题在于,当controll从支付网关站点返回站点时,永远不会调用函数check_callback

What I am doing wrong? 我做错了什么?

Any help will be appreciated. 任何帮助将不胜感激。

Thanks. 谢谢。

Payment gateways should be created as additional plugins that hook into WooCommerce. 付款网关应作为附加到WooCommerce的附加插件创建。 Inside the plugin, you need to create a class after plugins are loaded (or alternatively at initialisation if is on function.php file of your active theme, see below) . 在插件内部,您需要在插件加载后创建一个类(或者如果您处于活动主题的function.php文件中,则在初始化创建类,请参见下文)

So your code will look to this: 因此,您的代码将使用以下代码:

// for a plugin (the better choice)
add_action( 'plugins_loaded', 'init_mysolugion_gateway' );

// OR for theme function.php file
// add_action( 'init', 'init_mysolugion_gateway' );

function init_mysolugion_gateway() {

    class WC_Gateway_mysolugion extends WC_Payment_Gateway {

        public function __construct() {

            // ...
            // ...

            /* Hook IPN callback logic*/
            add_action( 'woocommerce_api_wc_gateway_mysolugion', array( $this, 'check_callback' ) );

        }

    }

}

As well as defining your class, you need to also tell WooCommerce (WC) that it exists. 在定义类的同时,还需要告诉WooCommerce(WC)它存在。 Do this by filtering woocommerce_payment_gateways: 通过过滤woocommerce_payment_gateways来做到这一点:

add_filter( 'woocommerce_payment_gateways', 'add_gateway_mysolugion' );
function add_gateway_mysolugion( $methods ) {
    $methods[] = 'WC_Gateway_mysolugion'; 
    return $methods;
}

Then you can add your callback function and this should work now: 然后,您可以添加您的回调函数,并且现在应该可以使用:

function check_callback() {
   // other code
}

References: 参考文献:

Thanks @LoicTheAztec, for replying and helping. 感谢@LoicTheAztec,感谢您的回复和帮助。 However, I found the reason why it was not working for me. 但是,我找到了它对我不起作用的原因。

In my case I moved the callback function out of the mail gateway class and it started working. 就我而言,我将回调函数从邮件网关类中移出,它开始起作用。 Simply put it in separate class and initiate that class in constructor of the main gateway class. 只需将其放在单独的类中,然后在主网关类的构造函数中初始化该类。 that's it. 而已。

cheers, 干杯,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM