简体   繁体   English

获取自定义支付网关数据作为Woocommerce 3中的设置

[英]Get custom payment gateway data as settings in Woocommerce 3

I'm making a custom payment gateway. 我正在制作自定义支付网关。 The complex parts are going fine but I've now been stuck on something stupid for hours. 复杂的部分进展顺利,但我现在已经陷入了几个小时愚蠢的事情。

I've created custom settings for the gateway without issue, they can be set and saved, but I can't figure out how to recall them in other functions. 我已经为网关创建了自定义设置而没有问题,可以设置和保存它们,但我无法弄清楚如何在其他功能中调用它们。

If I place var_dump($this->get_option('title')) within the custom gateway class (which extends WC_Payment_Gateway ) it will show correctly at the top of the settings page. 如果我将var_dump($this->get_option('title'))放在自定义网关类( extends WC_Payment_Gateway )中,它将在设置页面的顶部正确显示。 Anywhere else, it won't. 在其他地方,它不会。 I've tried now hundreds of things, like trying to access this class by $this = new WC_Custom_Gateway, making the functions involved public, and utilising init_settings() .. I'm sure there is a very simple solution, but as a beginner I just cannot see it. 我现在尝试了数百件事,比如尝试通过$ this = new WC_Custom_Gateway访问这个类,使函数公开,并使用init_settings() ..我确信有一个非常简单的解决方案,但作为一个初学者我只是看不到它。 I've tried examining the work of others to no avail also. 我试过检查别人的工作也无济于事。

How can I make those settings available from outside the class where they are defined in? 如何从定义它们的类外部提供这些设置?

Using the following code will allow you to display the necessary data from your payment gateway settings using WC_Payment_Gateways and WC_Payment_Gateway methods this way: 使用以下代码,您可以使用WC_Payment_GatewaysWC_Payment_Gateway方法以这种方式显示支付网关设置中的必要数据:

// HERE define you payment gateway ID (from $this->id in your plugin code)
$payment_gateway_id = 'bacs';

// Get an instance of the WC_Payment_Gateways object
$payment_gateways   = WC_Payment_Gateways::instance();

// Get the desired WC_Payment_Gateway object
$payment_gateway    = $payment_gateways->payment_gateways()[$payment_gateway_id];

// Display saved Settings example:
echo '<p>Title: ' . $payment_gateway->title . '</p>';
echo '<p>Description: ' . $payment_gateway->description . '</p>';
echo '<p>Instructions: ' . $payment_gateway->instructions . '</p>';

// Display all the raw data for this payment gateway 
echo '<pre>'; print_r( $payment_gateway ); echo '</pre>'; 

Alternatively you can also use this shorter way: 或者您也可以使用这种更短的方式:

// You will have to replace 'bacs' by your payment gateway ID (from $this->id in your plugin code)
$payment_gateway = WC()->payment_gateways->payment_gateways()['bacs'];

// and so on …

Tested and works. 经过测试和工作。

You can also use some WC_Payment_Gateway methods on $payment_gateway 您还可以在$payment_gateway上使用一些WC_Payment_Gateway方法

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

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