gpt4 book ai didi

php - 根据用户角色为 WooCommerce 付款方式分配百分比费用

转载 作者:行者123 更新时间:2023-12-04 08:23:43 25 4
gpt4 key购买 nike

如何扩展此代码段以包含另外 2 个不同费用的支付网关?
我要添加的支付网关是“cardgategiropay”和“cardgateideal”,费用分别为 3% 和 2% 。


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('order-pay') ) )
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', 'shop_manager');
$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 );
}
}
}

这段代码工作正常。只是检查我的语法是否正确。

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', 'shop_manager');
$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 );
}
if ($payment_method == 'cardgateideal'){
$percentage = 0.02;
$surcharge = (WC()->cart->cart_contents_total + WC()->cart->shipping_total) * $percentage;
$cart->add_fee( 'Card Fee (2%)', $surcharge, true );
}
if ($payment_method == 'cardgategiropay'){
$percentage = 0.03;
$surcharge = (WC()->cart->cart_contents_total + WC()->cart->shipping_total) * $percentage;
$cart->add_fee( 'Card Fee (3%)', $surcharge, true );
}
}
}

最佳答案

您也可以 array_intersect()检查用户角色,使用 IF/ELSE 语句,而不是只使用多个 IF 语句,并优化和压缩代码如下:

add_action('woocommerce_cart_calculate_fees', 'sm_credit_card_fee_role_gateway' );
function sm_credit_card_fee_role_gateway( $cart ){
if ( is_admin() && !defined('DOING_AJAX') )
return;

// Only on checkout page and logged in users
if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) || ! is_user_logged_in() )
return;

$wpuser_object = wp_get_current_user();
$allowed_roles = array('administrator', 'default_wholesaler', 'wholesaler-non-vat-registered', 'shop_manager');

if ( array_intersect($allowed_roles, $wpuser_object->roles) ){
$payment_method = WC()->session->get('chosen_payment_method');

if ( 'cardgatecreditcard' === $payment_method ){
$percentage = 8.5;
}
elseif ( 'cardgateideal' === $payment_method ){
$percentage = 2;
}
elseif ( 'cardgategiropay' === $payment_method ){
$percentage = 3;
}
}
if ( isset($percentage) ) {
$surcharge = ($cart->cart_contents_total + $cart->shipping_total) * $percentage / 100;
$cart->add_fee( sprintf( __('Card Fee (%s)', 'woocommerce'), $percentage . '%' ), $surcharge, true );
}
}
还缺少一些内容以允许刷新支付网关更改:
// jQuery - Update checkout on payment method change
add_action( 'wp_footer', 'custom_checkout_jquery_script' );
function custom_checkout_jquery_script() {
if ( ( is_checkout() && ! is_wc_endpoint_url() ) && is_user_logged_in() ) :
?>
<script type="text/javascript">
jQuery( function($){
$('form.checkout').on('change', 'input[name="payment_method"]', function(){
$(document.body).trigger('update_checkout');
});
});
</script>
<?php
endif;
}
代码位于事件子主题(或事件主题)的 functions.php 文件中。测试和工作。

相关: Add fee based on specific payment methods in WooCommerce

关于php - 根据用户角色为 WooCommerce 付款方式分配百分比费用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65375378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com