Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all enabled payment methods in Woocommerce

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 stucked with listing all the enabled Payment Methods. Does anyone know 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>

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.

I hope someone can help.

Thanks! Eli enter image description here

like image 743
elimariaaa Avatar asked Nov 30 '22 15:11

elimariaaa


1 Answers

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
like image 156
KMcAloon Avatar answered Dec 04 '22 10:12

KMcAloon