我在WooCommerce上建立了一个网上商店,既零售也批发。但是信用卡的费用太贵了。我很乐意为我的零售客户支付信用卡费用,但如果他们选择用信用卡支付,我想向他们收取费用。
我设法想出了下面的代码,效果非常好。但我有两种不同类型的经销商,所以我需要扩展代码,我不知道如何去做。
我试图扩展这段代码,以包括三个不同的用户角色:
角色1:未注册增值税的批发商角色2:默认批发商角色3:管理员
知道怎么做吗?
add_action( 'woocommerce_cart_calculate_fees', 'bbloomer_add_checkout_fee_for_gateway' );
function bbloomer_add_checkout_fee_for_gateway() {
if ( ! current_user_can( 'wholesaler-non-vat-registered' ) )
return;
global $woocommerce;
$chosen_gateway = $woocommerce->session->chosen_payment_method;
if ( $chosen_gateway == 'cardgatecreditcard' ) {
$percentage = 0.085;
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
$woocommerce->cart->add_fee( 'Credit Card Fee (8.5%)', $surcharge, true, '');
}
}
此代码也可以工作,但它也会向登录的客户添加信用卡费用。所以代码对客人有效,但是一旦客户登录他们的账户,他们就会被收取信用卡费用。
add_action( 'woocommerce_cart_calculate_fees', 'bbloomer_add_checkout_fee_for_gateway' );
function bbloomer_add_checkout_fee_for_gateway() {
global $woocommerce;
$user_role = wp_get_current_user();
$order_fee_roles = array( 'customer', 'wholesale', 'administrator' );
if (! is_user_logged_in() && !in_array($order_fee_roles, $user_role->roles ))
return;
$chosen_gateway = WC()->session->get( 'chosen_payment_method' );
if ( $chosen_gateway == 'cardgatecreditcard' ){
$percentage = 0.085;
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
$woocommerce->cart->add_fee( 'Credit Card Fee (8.5%)', $surcharge, true, '');
}}
add_action( 'woocommerce_review_order_before_payment', 'bbloomer_refresh_checkout_on_payment_methods_change' );
function bbloomer_refresh_checkout_on_payment_methods_change(){ ?>
<script type="text/javascript">
(function($){
$( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
$('body').trigger('update_checkout');
});
})(jQuery);
</script>
<?php
}
多亏了莎拉·麦克马洪,这里有了解决办法。工作起来很有魅力!
add_action('woocommerce_cart_calculate_fees', 'sm_credit_card_fee_role_gateway', 10, 1);
function sm_credit_card_fee_role_gateway($cart){
if (is_admin() && !defined('DOING_AJAX'))
return;
if (!(is_checkout() && !is_wc_endpoint_url()))
return;
if (!is_user_logged_in())
return;
$user = wp_get_current_user();
$roles = (array) $user->roles;
$roles_to_check = array('administrator', 'default_wholesaler', 'wholesaler-non-vat-registered');
$compare = array_diff($roles, $roles_to_check);
if (empty($compare)){
$payment_method = WC()->session->get('chosen_payment_method');
if ($payment_method == 'cardgatecreditcard'){
$percentage = 0.085;
$surcharge = (WC()->cart->cart_contents_total + WC()->cart->shipping_total) * $percentage;
$cart->add_fee( 'Credit Card Fee (8.5%)', $surcharge, true );
}
}
}
第二个代码的问题可能是您设置的特定条件。
$order_fee_roles=数组('客户','批发','管理员'); if(!is_user_logged_in()
你可以考虑把它们拆开,让它更容易测试。因此,首先检查用户是否已登录。然后检查他们是否已登录,然后检查用户角色。
您设置的条件的一个问题是,您说“如果用户没有登录,并且不是这些角色之一,那么就不要运行”。当用户注销时,他们没有角色,当用户登录时,这段代码不适用。这就是为什么它为每个人运行。要设置条件,以便检查用户是否登录,只需删除感叹号。这会将您的检查更改为'如果用户已登录,而不是以下角色之一'...
add_action('woocommerce_cart_calculate_fees', 'sm_credit_card_fee_role_gateway', 10, 1);
function sm_credit_card_fee_role_gateway($cart){
if (is_admin() && !defined('DOING_AJAX'))
return;
if (!(is_checkout() && !is_wc_endpoint_url()))
return;
if (!is_user_logged_in())
return;
$user = wp_get_current_user();
$roles = (array) $user->roles;
$roles_to_check = array('administrator', 'default_wholesaler', 'wholesaler-non-vat-registered');
$compare = array_diff($roles, $roles_to_check);
if (empty($compare)){
$payment_method = WC()->session->get('chosen_payment_method');
if ($payment_method == 'cardgatecreditcard'){
$percentage = 0.085;
$surcharge = (WC()->cart->cart_contents_total + WC()->cart->shipping_total) * $percentage;
$cart->add_fee( 'Credit Card Fee (8.5%)', $surcharge, true );
}
}
}