php 如何根据用户角色在WooCommerce中添加运费?

aiazj4mn  于 2023-04-10  发布在  PHP
关注(0)|答案(1)|浏览(130)

我正在尝试根据用户角色添加运输。
如何从不含税的总价中管理运输成本?
有一个批发用户类型(wholesale_customer),然后还有其余的用户角色(让我们称之为**“其余”),它们对每个人都有相同的规则。
批发用户的规则如下:航运是免费的,如果他们达到的金额
€70**,如果他们没有达到这个数额,航运成本将是€10,这是加到总
规则**“其余”是,航运是免费的,如果他们超过金额€30**,如果这个数额没有超过,运费将€10
我通过使用current_user_can()检查用户角色来获得此信息
对于用户谁不是批发商,(“其余”),一切都很好地与以下功能,因为产品的价格为这种类型的用户不携带税.可以功能:

add_filter('woocommerce_package_rates', 'shipping_costs_for_retailers', 10, 2);
function shipping_costs_for_retailers($rates, $package) {
    $user = wp_get_current_user();
    $user_roles = $user->roles;
    $subtotal = WC()->cart->subtotal;
    if (!in_array('wholesale_customer', $user_roles) && $subtotal < 30) {
        $new_rate = array();
        foreach ($rates as $rate_id => $rate) {
            $new_rate[$rate_id] = $rate;
            $new_rate[$rate_id]->cost += 5;
        }
        return $new_rate;
    }
    return $rates;
}

add_filter('woocommerce_cart_totals_shipping_html', 'message_for_retailers');
function message_for_retailers($html) {
    $user = wp_get_current_user();
    $user_roles = $user->roles;
    $subtotal = WC()->cart->subtotal;
    if (!in_array('wholesale_customer', $user_roles) && $subtotal < 30) {
        $html .= '<p>Shipping cost: $5.00</p>';
    }
    return $html;
}

问题来自批发商角色的用户,因为税被添加到产品的价格中。所以,有一个小的利润率给我错误。例如,如果金额为€68.40,运费被禁用,因为当添加税时,总金额超过€70,这是这些用户免费送货的限额。换句话说,我需要根据小计进行计算,不加税。
对于这种类型的用户,我使用了与“其余”用户相同的系统,我只是试图修改变量WC( )->cart->subtotal,但我不知道如何做到这一点,我真的寻找了想法和解决方案,但我找不到方法。这是我尝试管理批发用户运输成本的方法

add_filter('woocommerce_package_rates', 'shipping_ expenses_ for_wholesalers', 10, 2);
function shipping_ expenses_ for_wholesalers($rates, $package) {
    $user = wp_get_current_user();
    $user_roles = $user->roles;
    $subtotal = WC()->cart->subtotal;
    if (in_array('wholesale_customer', $user_roles) && $subtotal < 70) {
        $new_rate = array();
        foreach ($rates as $rate_id => $rate) {
            $new_rate[$rate_id] = $rate;
            $new_rate[$rate_id]->cost += 10;
        }
        return $new_rate;
    }
    return $rates;
}

add_filter('woocommerce_cart_totals_shipping_html', 'message_for_wholesalers');
function message_for_wholesalers($html) {
    $user = wp_get_current_user();
    $user_roles = $user->roles;
    $subtotal = WC()->cart->subtotal;
    if (in_array('wholesale_customer', $user_roles) && $subtotal < 70) {
        $html .= '<p>Shipping cost: $10.00</p>';
    }
    return $html;
}

我该怎么做才能纠正这个问题?
我应该如何修改我的功能来管理不含税的价格?
你可以看到发生在我身上的事情的截图,当批发客户,在小计没有达到70欧元,但运费被停用,因为总超过70欧元
谢谢你

rta7y2nd

rta7y2nd1#

你的问题又长又复杂,所以首先我指出我理解的事情,并用它来回答你的问题:
1.您正在尝试根据用户角色添加发货。
1.有一个批发用户类型(wholesale_customer),然后还有其他用户角色(我们称之为“The Rest”),每个人都有相同的规则。
1.批发用户的规则如下:航运是免费的,如果他们达到70欧元的数额,如果他们没有达到这个数额,运费将是10欧元,这是加到总数。
1.规则为“其余”是,航运是免费的,如果他们超过30欧元的数额,如果这个数额没有超过,航运费用将是5欧元
1.您需要根据小计进行此计算,而不添加税。
在我的回答之前,我澄清了一些关于你的代码的问题,你是在汽车中添加的所有费率中添加+10,例如,如果已经在购物车中添加了20,那么现在它是30,如果有第二次运输税15,那么它将是25,等等,所以10是在所有税中添加,但你在问题中说::“运费将是10欧元”,因此,我们添加一个零(0)所有订单的收费统一费率航运方法,我们添加10收费,我们需要的代码,我给所有步骤below
Step : 1
step : 2
step : 3
step : 4
step : 5
step : 6
step : 7
step : 8

<?php
add_filter( 'woocommerce_package_rates', 'custom_shipping_costs_based_on_user_role', 10, 2 );
function custom_shipping_costs_based_on_user_role( $rates, $package ) {
    // Get the current user role
    $current_user = wp_get_current_user();
    $user_role = $current_user->roles[0];

    // Get the order subtotal without taxes
    $subtotal = WC()->cart->subtotal_ex_tax;
    
    if(isset($rates['flat_rate:3'])) {

        // Set the shipping costs based on the user role and subtotal
        if ( $user_role == 'wholesale_customer' ) {
            if ( $subtotal >= 70 ) {
                // Free shipping
                unset( $rates['flat_rate:3'] ); // remove standard shipping rate
            } else {
                // Add €10 shipping cost
                $rates['flat_rate:3']->cost = 10;
            }
        } else {
            if ( $subtotal >= 30 ) {
                // Free shipping
                unset( $rates['flat_rate:3'] ); // remove standard shipping rate
            } else {
                // Add €10 shipping cost
                $rates['flat_rate:3']->cost = 5;
            }
        }
    }
    
    return $rates;
}

如果这不是你想的那样,那就告诉我,不要把它和你的问题代码混在一起,先去掉它们再用
注意:购物车不反映页面重新加载时的更改,它反映片段更改时的更改,因此在代码或设置发生任何更改后,更改产品数量或添加删除任何产品,以便您可以看到发生的更改

相关问题