4

I have this code to set WooCommerce variables

// Defining User set variables
$this->title = $this->get_option( 'title' );
$this->description = $this->get_option( 'description' );
$this->instructions = $this->get_option( 'instructions' );

but how to get $this->instructions in thankyou.php WooCommerce template?

I already tried using $order->instructions but then an errors appear

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.)

So I tried to see what inside $order, and then I see a long vars that doesn't have the text that I set for $this->instructions in WooCommerce Payment Gateway Plugin that I built myself.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

2 Answers2

4

You can get all Woocommerce payment methods with WC_Payment_Gateways class. Then you can get the checkout available payment methods and get the related data this way:

$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();
}

Tested and works in 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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • WOW that works like a magic! Thank you! did you know how to add a custom function for `$order`? because I want to add a price in `BTC`, so I need something like this `$order->get_price_in_btc()`. Thank you in advance – Khrisna Gunanasurya Jul 20 '18 at 06:26
  • yes, I already create that function, but the problem is I want to show it on the order data, because it depends on the customer wish, with what payment method, but if the customer is selecting the BTC payment, then the price in BTC must be stored as well, as the shop currency, for now I just inject 2 functions in `woocommerce/includes/class-wc-order.php` for set and get price in BTC props, but I'm not sure this is the correct way to do it, because when the plugin updated, those functions will be gone, so is there any way / reference you know? because I can't found them – Khrisna Gunanasurya Jul 20 '18 at 07:11
  • yes that's what I've been thinking, I will create new question then, thank you! – Khrisna Gunanasurya Jul 20 '18 at 08:00
  • 1
    sorry but can you help me with my question in here? https://stackoverflow.com/questions/51438137/add-custom-function-to-wc-order-in-woocommerce, Thank you in advance Loic – Khrisna Gunanasurya Jul 20 '18 at 08:23
  • 1
    @KhrisnaGunanasurya Yes, I will look at it. – LoicTheAztec Jul 20 '18 at 08:33
  • Hi, did you already see my new question? and if you have time, please help me, thank you – Khrisna Gunanasurya Jul 21 '18 at 02:50
0

The above example is great! Here's just another option to show the payment methods in radio inputs or dropdown. The shortcode is [display_payment_methods]. Add code to child theme functions.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 
  }