在 Woocommerce 中获取支付网关相关数据 [英] Get payment gateway related data in Woocommerce
问题描述
我有这个代码来设置 WooCommerce 变量
I have this code to set WooCommerce variables
// Defining User set variables
$this->title = $this->get_option( 'title' );
$this->description = $this->get_option( 'description' );
$this->instructions = $this->get_option( 'instructions' );
但是如何在thankyou.php
WooCommerce 模板中获取$this->instructions
?
but how to get $this->instructions
in thankyou.php
WooCommerce template?
我已经尝试过使用 $order->instructions
但随后出现错误
I already tried using $order->instructions
but then an errors appear
注意:指令调用不正确.订单属性应该不能直接访问.回溯:需要('wp-blog-header.php'),require_once('wp-includes/template-loader.php'),include('/themes/startup-company/page.php'), the_content,apply_filters('the_content'), WP_Hook->apply_filters, do_shortcode,preg_replace_callback, do_shortcode_tag, WC_Shortcodes::checkout,WC_Shortcodes::shortcode_wrapper, WC_Shortcode_Checkout::output,WC_Shortcode_Checkout::order_received, wc_get_template,include('/plugins/woocommerce/templates/checkout/thankyou.php'),WC_Abstract_Legacy_Order->__get, wc_doing_it_wrong 请看在 WordPress 中调试以获取更多信息.(此消息已添加在 3.0 版中.)
Notice: instructions was called incorrectly. Order properties should not be accessed directly. Backtrace: require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), include('/themes/startup-company/page.php'), the_content, apply_filters('the_content'), WP_Hook->apply_filters, do_shortcode, preg_replace_callback, do_shortcode_tag, WC_Shortcodes::checkout, WC_Shortcodes::shortcode_wrapper, WC_Shortcode_Checkout::output, WC_Shortcode_Checkout::order_received, wc_get_template, include('/plugins/woocommerce/templates/checkout/thankyou.php'), WC_Abstract_Legacy_Order->__get, wc_doing_it_wrong Please see Debugging in WordPress for more information. (This message was added in version 3.0.)
所以我试图查看 $order
中的内容,然后我看到一个长变量没有我为 $this->instructions
在我自己构建的 WooCommerce 支付网关插件中.
So I tried to see what inside $order
, and then I see a long vars that doesn't have the text that I set for $this->instructions
in WooCommerce Payment Gateway Plugin that I built myself.
推荐答案
您可以使用 WC_Payment_Gateways
类获取所有 Woocommerce 付款方式.然后您可以通过以下方式获取结帐可用付款方式并获取相关数据:
You can get all Woocommerce payment methods with WC_Payment_Gateways
class. Then you can get the checkout available payment methods and get the related data this way:
$wc_gateways = new WC_Payment_Gateways();
$payment_gateways = $wc_gateways->get_available_payment_gateways();
// Loop through Woocommerce available payment gateways
foreach( $payment_gateways as $gateway_id => $gateway ){
$title = $gateway->get_title();
$description = $gateway->get_description();
$instructions = property_exists( $gateway , 'instructions' ) ? $gateway->instructions : '';
$icon = $gateway->get_icon();
}
在 Woocommerce 3+ 中测试和工作
Tested and works in Woocommerce 3+
您还可以调用自定义支付网关类的实例,并在上面使用上面代码中的方法和属性……或者您可以使用 $gateway_id
$gateway_id在 foreach 循环内定位特定的支付网关IF
语句中的 code>.
You can also call an instance of your custom Payment gateway Class and use on it the methods and properties like in the code above… Or you can target a specific payment gateway inside the foreach loop using the
$gateway_id
in anIF
statement.
这篇关于在 Woocommerce 中获取支付网关相关数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!