phpwordpressmethodswoocommercepayment-gateway

Get payment gateway related data in Woocommerce


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.


Solution

  • 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+ (still works In lastest WooCommerce version 9.3.1)

    Or you can target a specific payment gateway inside the foreach loop using the $gateway_id in an IF statement.


    You can also call an instance of any Payment gateway Class and use on it any available method or property, like for example:

    $bacs_gateway = new WC_Gateway_BACS(); // Get an instance of the WC_Gateway_BACS object
    
    // Using methods
    $title        = $bacs_gateway->get_title(); // The title
    $description  = $bacs_gateway->get_description(); // The description
    
    // OR Using properties
    $title        = $bacs_gateway->title; // The title
    $description  = $bacs_gateway->description; // The description