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

标签 php wordpress woocommerce payment-gateway payment-method

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

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

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

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

最佳答案

使用以下代码,您可以使用 WC_Payment_Gateways 显示支付网关设置中的必要数据和 WC_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>'; 

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

// 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 …

经过测试并且有效。

You can also use some WC_Payment_Gateway methods on $payment_gateway

关于php - 获取自定义支付网关数据作为 Woocommerce 3 中的设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52844146/

相关文章:

php - 如何选择字段的默认值

php - 创建富文本编辑器界面

Wordpress:没有分配分类法的查询帖子

PHP 使用 <?php ?> 而不是 <? ?>

html - slider 中缩略图的 CSS 定位问题

javascript - 使用复选框中的 window.location.assign 添加多个 GET 参数到 URL

php - 自定义添加到购物车按钮以将多个产品添加到购物车中,数量为 :woocommerce

css - 如何在 woocommerce 中为产品列表制作固定列?

css - 在框中以全宽显示 Woocommerce 变体下拉列表

PHP PDO连接和sql查询