为了账号安全,请及时绑定邮箱和手机立即绑定

如果在WooCommerce中将优惠券应用于购物车,则仅对登录用户显示BACS支付网关

如果在WooCommerce中将优惠券应用于购物车,则仅对登录用户显示BACS支付网关

PHP
慕的地10843 2021-05-19 18:13:34
我拥有的代码可用于为客人和客户隐藏BACS支付网关,但是我需要对其进行更改,以便仅在客户/管理员在CART或CHECKOUT上应用称为FOOD的特定优惠券代码时,BACS网关才可用。换句话说:隐藏BACS网关,直到将名为FOOD的COUPON应用于CART或CHECKOUT。这是我的代码:add_filter('woocommerce_available_payment_gateways', 'show_bacs_if_coupon_is_used', 99, 1);function show_bacs_if_coupon_is_used( $available_gateways ) {        $current_user = wp_get_current_user();        if ( isset($available_gateways['bacs']) && (current_user_can('customer'))) {             unset($available_gateways['bacs']);             } else if ( isset($available_gateways['bacs']) && !is_user_logged_in())  {             unset($available_gateways['bacs']);         }         return $available_gateways;}
查看完整描述

1 回答

?
慕妹3242003

TA贡献1824条经验 获得超6个赞

仅当特定的优惠券仅适用于已登录用户的购物车时,才显示BACS付款方式(使用WC_Cart get_applied_coupons()方式):


add_filter('woocommerce_available_payment_gateways', 'show_bacs_for_specific_applied_coupon', 99, 1);

function show_bacs_for_specific_applied_coupon( $available_gateways ) {

    if ( is_admin() ) return $available_gateways; // Only on frontend


    $coupon_code = 'FOOD'; // <== Set here the coupon code


    if ( isset($available_gateways['bacs']) && ! ( is_user_logged_in() &&  

    in_array( strtolower($coupon_code), WC()->cart->get_applied_coupons() ) ) ) {

        unset($available_gateways['bacs']);

    }

    return $available_gateways;

}

代码进入您的活动子主题(或活动主题)的functions.php文件中。经过测试和工作。


查看完整回答
反对 回复 2021-05-28
  • 1 回答
  • 0 关注
  • 111 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信