我发现这个来自另一个post on Stackoverflow的精彩片段,根据类别向WooCommerce购物车添加自定义费用,但我无法将其修改为:
1)向多个类别申请注册费
2)根据购物车中同一类别的多种产品计算注册费
3)根据多种产品计算注册费,注册费用来自不同类别。
非常感谢任何帮助和/或建议。
*代码略有修改,以保持与我问的问题相关。
function my_reg_fee( $cart_object ) {
global $woocommerce;
$specialfeecat = 60; // category id for the special fee
$spfee = 85; // initialize special fee
foreach ( $cart_object->cart_contents as $key => $value ) {
$proid = $value['product_id']; //get the product id from cart
$quantiy = $value['quantity']; //get quantity from cart
$itmprice = $value['data']->price; //get product price
$terms = get_the_terms( $proid, 'product_cat' ); //get taxonamy of the prducts
if ( $terms && ! is_wp_error( $terms ) ) :
foreach ( $terms as $term ) {
$catid = $term->term_id;
if($specialfeecat == $catid ) {
$spfee = $spfee + $itmprice * $quantiy;
}
}
endif;
}
if($spfee > 0 ) {
$woocommerce->cart->add_fee( 'Registration Fee', $spfee, true, 'standard' );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'my_reg_fee' );