如何在WooCommerce结账页为自定义支付插件添加支付Logo
给WooCommerce自定义支付方式添加Logo的两种方法
方法一:在自定义支付网关类中直接设置(官方推荐)
这是WooCommerce标准的实现方式,直接在你的自定义支付网关类的构造函数里定义icon属性即可:
class WC_Custom_Payment_Gateway extends WC_Payment_Gateway { public function __construct() { // 基础配置 $this->id = 'custom_payment'; $this->method_title = '自定义支付方式'; $this->method_description = '这是我的自定义支付方式'; // 设置支付Logo路径 // 替换成你实际的图片地址,可根据资源位置用对应函数: // 主题目录用get_stylesheet_directory_uri(),插件目录用plugins_url() $this->icon = get_stylesheet_directory_uri() . '/images/custom-payment-logo.png'; // 其他必要配置 $this->supports = array('products'); $this->init_form_fields(); $this->init_settings(); $this->title = $this->get_option('title'); $this->description = $this->get_option('description'); add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options')); } // 实现process_payment等必要方法 }
注意:图片建议控制在48px以内高度,避免前台显示错位;路径可以用绝对URL,或者用WordPress内置函数调用对应目录资源。
方法二:通过functions.php用钩子修改
如果不想修改已有的支付网关类代码,可以用woocommerce_gateway_icon钩子针对性添加Logo:
add_filter('woocommerce_gateway_icon', 'add_custom_payment_gateway_icon', 10, 2); function add_custom_payment_gateway_icon($icon, $gateway_id) { // 替换为你的自定义支付网关ID if ($gateway_id === 'custom_payment') { $custom_icon = '<img src="' . get_stylesheet_directory_uri() . '/images/custom-payment-logo.png" alt="自定义支付Logo" />'; $icon = $custom_icon; } return $icon; }
该方法会替换指定网关的原有图标(如果存在),适合后期补加或修改图标场景。
内容的提问来源于stack exchange,提问作者Dev.S
相关产品推荐
相关产品推荐

