我需要根据购买者的角色添加两种不同的运费。我有一个名为'wholesale_customer'的角色,不需要支付运费。然而,如果购物车的小计等于或小于90欧元,必须阻止购买并在结账时添加一条消息。(请记住,您必须达到90欧元的金额)
另一种选择是其他所有WordPress角色,如果他们的购物车小计为40欧元或更少,则需要支付5欧元的运费。
我已经在WooCommerce中配置了运费,并创建了两种最低订单金额的免费运费方式,分别为40欧元和90欧元。
问题是,当具有'wholesale_customer'角色的用户达到40欧元时,他也会启用该免费运费方式,这是不应该发生的,因为对于这个用户角色('wholesale_customer'),只有当购买金额超过90欧元时才免费。
我已经按照以下方式在WooCommerce中配置了运费,创建了2种运费方式: - 一种的“最低所需数量”为90欧元 - 另一种的“最低所需数量”为40欧元 - 运费为0欧元。
我尝试添加以下功能以获得所需结果,但所有的运输方式都一直处于启用状态,因此“最低40欧元免费运输”的零售运输方式也对具有批发客户角色的用户生效。这是不应该发生的,因为具有这个角色的用户应该享受到属于他们的特权。
我展示了一些我用来尝试实现目标的代码,因为我进行了很多测试。只是我还没有找到添加我在演示中提到的文本的方法,以适用于“批发客户”角色。
显示WooCommerce设置的图片。
另一种选择是其他所有WordPress角色,如果他们的购物车小计为40欧元或更少,则需要支付5欧元的运费。
我已经在WooCommerce中配置了运费,并创建了两种最低订单金额的免费运费方式,分别为40欧元和90欧元。
问题是,当具有'wholesale_customer'角色的用户达到40欧元时,他也会启用该免费运费方式,这是不应该发生的,因为对于这个用户角色('wholesale_customer'),只有当购买金额超过90欧元时才免费。
我已经按照以下方式在WooCommerce中配置了运费,创建了2种运费方式: - 一种的“最低所需数量”为90欧元 - 另一种的“最低所需数量”为40欧元 - 运费为0欧元。
我尝试添加以下功能以获得所需结果,但所有的运输方式都一直处于启用状态,因此“最低40欧元免费运输”的零售运输方式也对具有批发客户角色的用户生效。这是不应该发生的,因为具有这个角色的用户应该享受到属于他们的特权。
我展示了一些我用来尝试实现目标的代码,因为我进行了很多测试。只是我还没有找到添加我在演示中提到的文本的方法,以适用于“批发客户”角色。
显示WooCommerce设置的图片。
function custom_shipping_methods_visibility( $available_methods ) {
$subtotal = WC()->cart->subtotal;
$user = wp_get_current_user();
$user_roles = $user->roles;
// Check user role and cart subtotal to show/hide shipping methods
if ( in_array( 'wholesale_customer', $user_roles ) && $subtotal <= 70 ) {
unset( $available_methods['flat_rate:13'] );
} elseif ( $subtotal <= 30 ) {
unset( $available_methods['flat_rate:9'] );
}
return $available_methods;
}
add_filter( 'woocommerce_package_rates', 'custom_shipping_methods_visibility', 10, 1 );
////////////////////
// Adds a custom function to filter shipping methods based on cart role and subtotal
function custom_shipping_methods( $available_methods ) {
// Get cart subtotal
$subtotal = WC()->cart->subtotal;
// Gets the role of the current user
$user = wp_get_current_user();
$user_roles = $user->roles;
//Check user role and cart subtotal to adjust shipping methods
if ( in_array( 'wholesale_customer', $user_roles ) && $subtotal < 70 ) {
foreach ( $available_methods as $method_id => $method ) {
// Hide the shipping methods if the user is a wholesaler and the subtotal is less than €70
unset( $available_methods[ $method_id ] );
}
// Show a warning message
wc_add_notice( 'Debes alcanzar un mínimo de 70€ en tu carrito para realizar el pedido.', 'error' );
}
return $available_methods;
}
add_filter( 'woocommerce_package_rates', 'custom_shipping_methods', 10, 1 );
配置-1
配置-2:配置-3: