最近,我在这个Woocommerce项目中尝试使用一个代码段中的2个代码段,在该项目中,我需要为特定的产品类别(条款ID:19)和特定的国家/地区州(佛罗里达州'FL')设置费用
此外,我还需要将此费用乘以该产品类别中的商品(19)。
这是我的实际代码:
SELECT Students.Name, Assignments.Assignments, Assignments.Assigned,Assignments.Due
FROM students, assignments;
如何根据特定类别的商品和特定州的客户设置累进费用?
任何帮助将不胜感激。
答案 0 :(得分:0)
存在一些错误,您的代码有些过时了……以下代码将根据特定产品类别和特定状态下的购物车商品数量设置累进费用。
add_action( 'woocommerce_cart_calculate_fees', 'wc_custom_surcharge', 20, 1 );
function wc_custom_surcharge( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return; // Exit
## Your Settings (below) ##
$categories = array(19);
$targeted_states = array('FL');
$base_rate = 1;
$user_state = WC()->customer->get_shipping_state();
$user_state = empty($user_state) ? WC()->customer->get_billing_state() : $user_state;
$surcharge = 0; // Initializing
// If user is not from florida we exit
if ( ! in_array( $user_state, $targeted_states ) )
return; // Exit
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ){
// calculating fee based on the defined rate and on item quatinty
$surcharge += $cart_item['quantity'] * $base_rate;
}
}
// Applying the surcharge
if ( $surcharge > 0 ) {
$cart->add_fee( __("State Tire Fee", "woocommerce"), $surcharge, true );
}
}
代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。