列出 Woocommerce 中所有启用的付款方式

Posted

技术标签:

【中文标题】列出 Woocommerce 中所有启用的付款方式【英文标题】:List all enabled payment methods in Woocommerce 【发布时间】:2017-06-15 12:31:08 【问题描述】:

我正在努力将管理端的订单页面模仿到网站的 UI。这就像完全抄袭后端的内容。

我一直坚持列出所有已启用的付款方式。有谁知道我该如何实现它?

这是我的代码:

<?php
global $woocommerce;
wp_enqueue_script('jquery');
wp_enqueue_script('jquery-ui-core');
wp_enqueue_script('jquery-ui-datepicker');
wp_enqueue_style('jquery-ui-css', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/smoothness/jquery-ui.css');

if(isset( $_GET['status_id'] ))
    $update_order_id = $_GET['status_id'];

$data = array(
        'order_id' => $update_order_id
    );
// $result = wc_get_order_id_by_order_key();
$result = new WC_Order($update_order_id);
$order_date = explode(' ',$result->order_date);
$order_time = explode(':', $order_date[1]);
$order_time_hour = $order_time[0];
$order_time_minute = $order_time[1];
$order_date = $order_date[0];
$order_time_minute = $order_time[1];
$status = $result->post_status;
// print_r($result->get_payment_method());
$customer = new WC_Customer($update_order_id);
$payment_method_title = $result->payment_method_title;
$customer_email = $result->billing_email;
$customer_number = $result->customer_user;
$customer_first_name = $result->shipping_first_name;
$customer_details = get_post_meta($update_order_id);
// $customer_payment_title = $result->payment_method_title;
$customer_payment_title = get_post_meta($update_order_id,'_payment_method_title',true);
//print_r($customer_details);
$shipping_flight_number = $customer_details['shipping_flight_number'][0];
$shipping_flight_date = $customer_details['shipping_flight_date'][0];
$billing_first_name = $customer_details['_billing_first_name'][0];
$billing_last_name = $customer_details['_billing_last_name'][0];
$billing_company = $customer_details['_billing_company'][0];
$billing_address_1 = $customer_details['_billing_address_1'][0];
$billing_address_2 = $customer_details['_billing_address_2'][0];
$billing_city = $customer_details['_billing_city'][0];
$billing_postcode = $customer_details['_billing_postcode'][0];
$billing_country = $customer_details['_billing_country'][0];
$billing_email = $customer_details['_billing_email'][0];
$billing_phone = $customer_details['_billing_phone'][0];
$ip_address = $result->customer_ip_address;
$countries_obj   = new WC_Countries();
$countries   = $countries_obj->__get('countries');
$default_country = $countries_obj->get_base_country();
$default_county_states = $countries_obj->get_states( $default_country );
$customer_county = $result->billing_state;
$payment_gateways_obj = new WC_Payment_Gateways();
$enabled_payment_gateways = $payment_gateways_obj->payment_gateways();
// print_r($enabled_payment_gateways);
?>
<div class="entry-content">
<h3>Order #<?php echo $update_order_id; ?> details</h3>
<h4>Payment via <?php echo $payment_method_title; ?>. Customer IP: <?php echo $ip_address; ?></h4>
<div class="row">
    <div class="details-holder">
        <div class="col-md-4 details">
            <label>General Details</label>
            <form class="form-horizontal">
                <div class="form-group">
                <label for="OrderDate" class="col-sm-12 control-label to-the-left">Order Date:</label>
                    <div class="col-sm-12">
                        <input type="text" class="col-sm-4" id="date" name="date" value="<?php echo ($order_date != '') ? $order_date: ''; ?>"/>@
                        <input type="number" class="hour" placeholder="h" name="order_date_hour" id="order_date_hour" min="0" max="23" step="1" value="<?php echo ($order_time_hour != '')? $order_time_hour: ''; ?>" pattern="([01]?[0-9]1|2[0-3]1)">
                        :
                        <input type="number" class="minute" placeholder="m" name="order_date_minute" id="order_date_minute" min="0" max="59" step="1" value="<?php echo ($order_time_minute != '') ? $order_time_minute:''; ?>" pattern="[0-5]1[0-9]1">
                    </div>
                </div>
                <div class="form-group">
                    <label for="OrderStatus" class="col-sm-12 control-label to-the-left">Order Status:</label>
                    <?php $statuses = wc_get_order_statuses(); ?>
                    <div class="col-sm-12">
                    <select name="orderstatus" class="form-control custom-select col-sm-4">
                        <?php foreach( $statuses as $skey => $status ) : ?>
                            <option <?php echo ( ( isset( $status ) && $status == $skey ) ? 'selected="selected"' : '' )?> value="<?php echo $skey;?>"><?php echo $status; ?></option>
                        <?php endforeach; ?>
                    </select>
                    </div>
                </div>
                <div class="form-group">
                    <label for="CustomerInfo" class="col-sm-12 control-label to-the-left">Customer:</label>
                    <div class="col-sm-12">
                        <?php
                            if($customer_number == 0)
                                echo "Guest";
                             else 
                        ?>
                        <?php echo ($customer_first_name != '') ? $customer_first_name: ''; ?> (#<?php echo ($customer_number !='') ? $customer_number:''; ?> - <?php echo ($customer_email != '') ? $customer_email:'' ; ?>)
                        <?php 
                            
                        ?>
                    </div>
                </div>
            </form>
                <div class="form-group">
                    <label>Additional Details</label>
                </div>
                <div class="form-group">
                    <label>Travel Details</label>
                </div>
                <label for="FlightNumber" class="col-sm-12 control-label to-the-left">Fligh Number: <?php echo $shipping_flight_number; ?></label>
                <label for="FlightDate" class="col-sm-12 control-label to-the-left">Fligh Date: <?php echo $shipping_flight_date; ?></label>
        </div>
    </div>
    <div class="details-holder">
        <div class="col-md-4 details">
            <label>Billing Details</label>
            <form class="form-horizontal">
                <div class="form-group">
                    <label for="BillingFirstName" class="col-sm-6 control-label to-the-left">First Name:</label>
                    <label for="BillingLastName" class="col-sm-6 control-label to-the-left">Last Name:</label>
                    <div class="col-sm-6">
                        <input type="text" class="col-sm-12" name="billing_first_name" value="<?php echo ($billing_first_name != '') ? $billing_first_name: ''; ?>" />
                    </div>
                    <div class="col-sm-6">
                        <input type="text" class="col-sm-12" name="billing_last_name" value="<?php echo ($billing_last_name != '') ? $billing_last_name: ''; ?>" />
                    </div>
                </div>
                <div class="form-group">
                    <label for="BillingCompany" class="col-sm-12 control-label to-the-left">Company:</label>
                    <div class="col-sm-12">
                        <input type="text" class="col-sm-12" name="billing_company" value="<?php echo ($billing_company != '') ? $billing_company: ''; ?>" />
                    </div>
                </div>
                <div class="form-group">
                    <label for="BillingAddress1" class="col-sm-6 control-label to-the-left">Address 1:</label>
                    <label for="BillingAddress2" class="col-sm-6 control-label to-the-left">Address 2:</label>
                    <div class="col-sm-6">
                        <input type="text" class="col-sm-12" name="billing_address_1" value="<?php echo ($billing_address_1 != '') ? $billing_address_1: ''; ?>" />
                    </div>
                    <div class="col-sm-6">
                        <input type="text" class="col-sm-12" name="billing_address_2" value="<?php echo ($billing_address_2 != '') ? $billing_address_2: ''; ?>" />
                    </div>
                </div>
                <div class="form-group">
                    <label for="BillingCity" class="col-sm-6 control-label to-the-left">City:</label>
                    <label for="BillingPostcode" class="col-sm-6 control-label to-the-left">Postcode:</label>
                    <div class="col-sm-6">
                        <input type="text" class="col-sm-12" name="billing_city" value="<?php echo ($billing_city != '') ? $billing_city: ''; ?>" />
                    </div>
                    <div class="col-sm-6">
                        <input type="text" class="col-sm-12" name="billing_postcode" value="<?php echo ($billing_postcode != '') ? $billing_postcode: ''; ?>" />
                    </div>
                </div>
                <div class="form-group">
                    <label for="BillingCountry" class="col-sm-6 control-label to-the-left">Country:</label>
                    <label for="BillingState" class="col-sm-6 control-label to-the-left">State/County:</label>
                    <div class="col-sm-6">
                        <select class="form-control col-sm-12">
                            <?php foreach( $countries as $skey => $country ) : ?>
                                <option <?php echo ( ( $billing_country == $skey ) ? 'selected="selected"' : '' )?> value="<?php echo $skey;?>"><?php echo $country; ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="col-sm-6">
                        <select class="form-control col-sm-12">
                            <?php foreach( $default_county_states as $skey => $county ) : ?>
                                <option <?php echo ( ( $customer_county == $skey ) ? 'selected="selected"' : '' )?> value="<?php echo $skey;?>"><?php echo $county; ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label for="BillingEmail" class="col-sm-6 control-label to-the-left">Email:</label>
                    <label for="BillingPhone" class="col-sm-6 control-label to-the-left">Phone:</label>
                    <div class="col-sm-6">
                        <input type="text" class="col-sm-12" name="billing_email" value="<?php echo ($billing_email != '') ? $billing_email: ''; ?>" />
                    </div>
                    <div class="col-sm-6">
                        <input type="text" class="col-sm-12" name="billing_phone" value="<?php echo ($billing_phone != '') ? $billing_phone: ''; ?>" />
                    </div>
                </div>
                <div class="form-group">
                    <label for="BillingPaymentTitle" class="col-sm-6 control-label to-the-left">Payment Method:</label>
                    <div class="col-md-12">
                        <input class="col-sm-12" type="text" name="billing_payment_title" value="<?php echo ($customer_payment_title != '') ? $customer_payment_title : ''; ?>">
                    </div>
                </div>
            </form>
        </div>
    </div>
    <div class="details-holder">
        <div class="col-md-4 details">
            <label>Shipping Details</label>

        </div>
    </div>
</div>

我拥有的是:

$payment_gateways_obj = new WC_Payment_Gateways(); $enabled_pa​​yment_gateways = $payment_gateways_obj->payment_gateways();

其中,如果我print_r($enabled_payment_gateways),则显示所有支付网关数据。但我只需要启用的支付网关。

希望有人能帮忙。

谢谢! 伊莱

【问题讨论】:

【参考方案1】:

不知道这是否仍然需要,但每个网关对象都有一个您可以检查的enabled 属性。见https://docs.woocommerce.com/wc-apidocs/class-WC_Payment_Gateway.html

所以可能是这样的:

$gateways = WC()->payment_gateways->get_available_payment_gateways();
$enabled_gateways = [];

if( $gateways ) 
    foreach( $gateways as $gateway ) 

        if( $gateway->enabled == 'yes' ) 

            $enabled_gateways[] = $gateway;

        
    


print_r( $enabled_gateways ); // Should return an array of enabled gateways

【讨论】:

更好的是使用is_available()方法,它也检查网关是否启用。 绝对不需要检查【参考方案2】:

我在寻找相同的答案时遇到了这篇文章并找到了解决方案。这是在无线电输入或下拉菜单中显示付款方式的选项。简码是[display_payment_methods]。将代码添加到子主题 function.php 或使用代码 sn-p 插件。将简码放在页面/帖子中以在前端查看。

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 
  

下面列出的是我在 stackerverflow 上找到的另一个解决方案。这是帖子的链接:get-payment-gateway-related-data-in-woocommerce。

$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 中所有启用的付款方式的主要内容,如果未能解决你的问题,请参考以下文章

在 WooCommerce 中禁用基于用户国家地理 IP 的所有付款方式

根据 Woocommerce 中选择的付款方式禁用运输方式

在 WooCommerce 中禁用基于自定义运输方式的付款方式

添加一个结帐复选框字段,在 Woocommerce 中启用百分比费用

在 WooCommerce 中隐藏运输和付款方式

在 WooCommerce 中根据产品类型隐藏付款方式