将图标添加到自定义woocommerce支付网关

时间:2016-11-21 15:19:23

标签: wordpress woocommerce hook-woocommerce

我想在我的支付网关中添加自定义图标。我已经阅读了WOO网关api并且没有任何帮助。这是我的代码。请帮我找一个包含图标的功能方法,这样我的前端就有了一个图标。感谢

<?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 个答案:

答案 0 :(得分:1)

尝试使用反斜杠而不是斜杠,而不将初始空字符串与变量$plugin_dir

连接起来
$this->icon = apply_filters( 'woocommerce_gateway_icon', $plugin_dir.'\assets\paysecure.png' );

答案 1 :(得分:1)

使用WooCommerce过滤器woocommerce_gateway_icon,您可以通过以下方式将图标添加到付款网关:

/**
*  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 );

答案 2 :(得分:1)

我建议使用woocommerce_available_payment_gateways过滤器。

/**
 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' );

答案 3 :(得分:0)

尝试一下:

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