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

问题描述 投票:0回答:2

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

我不得不列出所有已启用的付款方式。我怎样才能实现它?

这是我的代码:

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

我有的是:

$payment_gateways_obj = new WC_Payment_Gateways();
$enabled_payment_gateways = $payment_gateways_obj->payment_gateways();

其中,如果我

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

php wordpress payment-gateway woocommerce-rest-api
2个回答
22
投票

不知道这是否仍然需要,但每个网关对象都有一个

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

1
投票

我看到这篇文章寻找相同的答案并找到了解决方案。这是在无线电输入或下拉列表中显示付款方式的选项。短代码是

[display_payment_methods]
。将代码添加到子主题 function.php 或使用代码片段插件。将短代码放在页面/帖子中以在前端查看。

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-lated-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();
}
© www.soinside.com 2019 - 2024. All rights reserved.