首页 文章

在Woocommerce中获取支付网关相关数据

提问于
浏览
1

我有这个代码来设置WooCommerce变量

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

我已经尝试使用 $order->instructions ,但随后出现错误

注意:说明调用不正确 . 不应直接访问订单属性 . 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请参阅WordPress中的调试以获取更多信息 . (此消息是在3.0版中添加的 . )

所以我试着看看 $order 里面是什么,然后我看到一个很长的变量,它没有我在我自己构建的WooCommerce Payment Gateway插件中为 $this->instructions 设置的文本 .

1 回答

  • 1

    您可以使用 WC_Payment_Gateways 类获得所有Woocommerce付款方式 . 然后你可以得到结账 available 付款方式并以这种方式获取相关数据:

    $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中工作

    您还可以调用自定义支付网关类的实例,并在其上使用上述代码中的方法和属性...或者您可以使用IF语句中的$ gateway_id定位foreach循环内的特定支付网关 .

相关问题