在 Woocommerce 付款方式标题上添加图片

Has*_*mal 3 php wordpress checkout payment-gateway woocommerce

在 Woocommerce 中,我计划在银行名称插件 BACS 之前添加图像。现在我已经输入了银行名称和其他设置,并且已经尝试在银行名称之前输入 HTML,但它不起作用。

Loi*_*tec 7

您可以轻松地在结帐页面的支付网关中添加图标(图像)。

但在Woocommerce该图标位于的称号。 要在标题之前更改它,您应该需要checkout/payment-method.php在此27编辑相关模板:

      <?php echo $gateway->get_title(); ?> <?php echo $gateway->get_icon(); ?>
Run Code Online (Sandbox Code Playgroud)

对此:

      <?php echo $gateway->get_icon(); ?> <?php echo $gateway->get_title(); ?>
Run Code Online (Sandbox Code Playgroud)

并保存……请参阅:如何通过主题覆盖 WooCommerce 模板……

例如,您需要将主题文件夹中的图像作为“资产”上传。

对于每个网关,您可以启用自定义图像,或返回默认图像,使用此自定义功能挂钩在woocommerce_gateway_icon动作挂钩中:

add_filter( 'woocommerce_gateway_icon', 'custom_payment_gateway_icons', 10, 2 );
function custom_payment_gateway_icons( $icon, $gateway_id ){

    foreach( WC()->payment_gateways->get_available_payment_gateways() as $gateway )
        if( $gateway->id == $gateway_id ){
            $title = $gateway->get_title();
            break;
        }

    // The path (subfolder name(s) in the active theme)
    $path = get_stylesheet_directory_uri(). '/assets';

    // Setting (or not) a custom icon to the payment IDs
    if($gateway_id == 'bacs')
        $icon = '<img src="' . WC_HTTPS::force_https_url( "$path/bacs.png" ) . '" alt="' . esc_attr( $title ) . '" />';
    elseif( $gateway_id == 'cheque' )
        $icon = '<img src="' . WC_HTTPS::force_https_url( "$path/cheque.png" ) . '" alt="' . esc_attr( $title ) . '" />';
    elseif( $gateway_id == 'cod' )
        $icon = '<img src="' . WC_HTTPS::force_https_url( "$path/cod.png" ) . '" alt="' . esc_attr( $title ) . '" />';
    elseif( $gateway_id == 'ppec_paypal' || 'paypal' )
        return $icon;

    return $icon;
}
Run Code Online (Sandbox Code Playgroud)

代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中。

在 WooCommerce 3 上测试并有效。


如何获取网关 ID
转到网关 ID 列中列出的WC 设置 > 结帐(页面末尾)

在此处输入图片说明