2
votes

I would like to add a custom icon to my payment gateway. I have read the WOO gateway API and have zero help. This is my code below. please help me find a functional way to include the icon so I have an icon on my front end. Thanks

<?php if ( ! defined( 'ABSPATH' ) ) { exit; }

add_filter( 'woocommerce_payment_gateways', 'init_wpuw_gateway' );
function init_wpuw_gateway ( $methods ) 
{
    $methods[] = 'WC_Gateway_WPUW'; 
    return $methods;
}


if( class_exists('WC_Payment_Gateway') ):
class WC_Gateway_WPUW extends WC_Payment_Gateway {

    /**
     * Constructor for the gateway.
     */
    public function __construct() {

        $plugin_dir = plugin_dir_url(__FILE__);

        $this->id                 = 'wpuw';

        //If you want to show an image next to the gateway’s name on the frontend, enter a URL to an image.
        $this->icon               = apply_filters( 'woocommerce_gateway_icon', ''.$plugin_dir.'/assets/paysecure.png' );
        $this->method_title       = __( 'User Wallet', 'woocommerce' );
        $this->method_description = __( 'Have your customers pay with their user wallet balance.', 'woocommerce' );
        $this->has_fields         = false;
4

4 Answers

2
votes

Try with backslash instead of slash without concatenating the initial empty string with the variable $plugin_dir

$this->icon = apply_filters( 'woocommerce_gateway_icon', $plugin_dir.'\assets\paysecure.png' );
2
votes

Using the WooCommerce filter woocommerce_gateway_icon you can add the icon to the payment gateway this way:

/**
*  Add Custom Icon 
*/ 

function custom_gateway_icon( $icon, $id ) {
    if ( $id === 'custom' ) {
        return '<img src="' . plugins_url( 'img/custom.png', __FILE__ ) . '" > '; 
    } else {
        return $icon;
    }
}
add_filter( 'woocommerce_gateway_icon', 'custom_gateway_icon', 10, 2 );
1
votes

I would suggest woocommerce_available_payment_gateways filter.

/**
 Add Custom Icon For Cash On Delivery
**/ 
function cod_gateway_icon( $gateways ) {
    if ( isset( $gateways['cod'] ) ) {
        $gateways['cod']->icon = get_stylesheet_directory_uri() . '/images/cod.png';
    }

    return $gateways;
}

add_filter( 'woocommerce_available_payment_gateways', 'cod_gateway_icon' );
0
votes

try this:

$this->icon = trailingslashit( WP_PLUGIN_URL ) . plugin_basename( dirname( __FILE__ ) ) . '/assets/paysecure.png';