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

Wil*_*son 2 php wordpress payment-gateway woocommerce payment-method

我正在制作一个自定义支付网关。复杂的部分进展顺利,但我现在已经被困在愚蠢的事情上好几个小时了。

我已经为网关创建了自定义设置,没有问题,可以设置和保存它们,但我不知道如何在其他功能中调用它们。

如果我放置var_dump($this->get_option('title'))在自定义网关类(which extends WC_Payment_Gateway)中,它将正确显示在设置页面的顶部。在其他任何地方,它都不会。我现在已经尝试了数百种方法,比如尝试通过 $this = new WC_Custom_Gateway 访问这个类,将所涉及的函数公开,并利用init_settings().. 我确定有一个非常简单的解决方案,但作为初学者我就是不能看见。我也试过检查其他人的工作也无济于事。

如何使这些设置在定义它们的类之外可用?

Loi*_*tec 5

使用以下代码将允许您以这种方式使用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>'; 
Run Code Online (Sandbox Code Playgroud)

或者,您也可以使用这种较短的方式:

// 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 …
Run Code Online (Sandbox Code Playgroud)

测试和工作。

您还可以使用一些WC_Payment_Gateway 方法$payment_gateway