List all enabled payment methods in WooCommerce
Asked Answered
W

2

9

I'm working on mimicking the Orders Page in the Admin side to the UI of the site. It's like a complete rip off of what's on the backend.

I'm stuck with listing all the enabled Payment Methods. How can I achieve it?

Here's my code:

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

The one I have is:

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

Wherein, if I print_r($enabled_payment_gateways), it shows all the payment gateways data. But I just need the payment gateways which are enabled.

enter image description here

Wheelbase answered 30/1, 2017 at 11:8 Comment(0)
B
22

Don't know if this is still needed, but each gateway object has an enabled property you can check against. See https://docs.woocommerce.com/wc-apidocs/class-WC_Payment_Gateway.html

So maybe something like this:

$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
Burbage answered 5/1, 2018 at 18:47 Comment(5)
Even better is to use the method is_available() which also checks if the gateway is enabled.Rathe
Definitely not needed to check thatBalmoral
Does anyone know what is the value of card payment option? Is it 'card' ?Felten
@Raymond: a quick scan of the docs suggests the gateway list is made up of the names of the gateways, so the name would be more like "Stripe" rather than "card", even if a card was being used. Which card payment gateway are you using?Equisetum
@Equisetum I'm using first option "Credit / Debit Card (required)"Felten
K
1

I came across this post looking for the same answer and found a solution. Here's option to show the payment methods in radio inputs or dropdown. The shortcode is [display_payment_methods]. Add code to child theme function.php or use code snippet plugin. Place shortcode in page/post to view on frontend.

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 
  }

Listed below is another solution I found on stackerverflow. Here's the link to the post: 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();
}
Knap answered 9/12, 2021 at 23:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.