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

Get payment gateway related data in Woocommerce

我有这段代码可以设置 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 但随后出现错误

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 里面的内容,然后我看到一个很长的变量,其中没有我在自己构建的 WooCommerce 支付网关插件中为 $this->instructions 设置的文本.

您可以使用 WC_Payment_Gateways class 获取所有 Woocommerce 支付方式。然后就可以得到checkout available payment methods 并通过这种方式获取相关数据:

$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+ 中测试和工作

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 an IF statement.

上面的例子很棒!这只是在无线电输入或下拉列表中显示付款方式的另一种选择。简码是 [display_payment_methods]。将代码添加到子主题 functions.php 或使用代码片段插件。将短代码放入 page/post 以在前端查看。

add_shortcode('display_payment_methods','display_payment_methods');  
function display_payment_methods(){
    global $woocommerce;


$available_gatewayz = WC()->payment_gateways->get_available_payment_gateways();

if ( $available_gatewayz ) { ?>
    <form id="add_payment_method" method="post">
        <div id="payment" class="woocommerce-Payment">
            <ul class="woocommerce-PaymentMethods payment_methods methods">
                <?php
                // Chosen Method.
                if ( count( $available_gatewayz ) ) {
                    current( $available_gatewayz )->set_current();
                }

                foreach ( $available_gatewayz as $gatewayz ) {
                    
                ?>
                    <li class="woocommerce-PaymentMethod woocommerce-PaymentMethod--<?php echo esc_attr( $gatewayz->id ); ?> payment_method_<?php echo esc_attr( $gatewayz->id ); ?>">
                        <input id="payment_method_<?php echo esc_attr( $gatewayz->id ); ?>" type="radio" class="input-radio" name="payment_method" value="<?php echo esc_attr( $gatewayz->id ); ?>" <?php checked( $gatewayz->chosen, true ); ?> />
                        <label for="payment_method_<?php echo esc_attr( $gatewayz->id ); ?>"><?php echo wp_kses_post( $gatewayz->get_title() ); ?> <?php echo wp_kses_post( $gatewayz->get_icon() ); ?></label>
                        <?php
                        if ( $gatewayz->has_fields() || $gatewayz->get_description() ) {
                            echo '<div class="woocommerce-PaymentBox woocommerce-PaymentBox--' . esc_attr( $gatewayz->id ) . ' payment_box payment_method_' . esc_attr( $gatewayz->id ) . '" style="display: none;">';
                            $gatewayz->payment_fields();
                            echo '</div>';
                        }
                        ?>
                    </li>
                    <?php
                }}  
?>
            </ul>

<!-- Enabled Payment Methods Dropdown Select -->
      <select name="payment_method" class="select_field"> 
              <option selected="selected" disabled="disabled" value="<?php echo esc_attr( $gatewayz->id ); ?>"><?php echo esc_attr( __( 'Select Payment Method' ) ); ?></option> 
              <?php
                    $available_gatewayz = WC()->payment_gateways->get_available_payment_gateways();

          // Chosen Method.
                if ( count( $available_gatewayz ) ) {
                    current( $available_gatewayz )->set_current();
                }

                foreach ( $available_gatewayz as $gatewayz ) {
                      $option = '<option value="' . esc_attr( $gatewayz->id)  . '" ';
                      $option .= (  esc_attr( $gatewayz->id)   ==  $available_gatewayz ) ? 'selected="selected"' : '';
                      $option .= '>';
                      $option .= wp_kses_post( $gatewayz->get_title() ) ;
                      $option .= '</option>';
                      echo $option;
                  }
                    
                
              ?>
          </select>
  
  <?php 
  }