我试图添加两个费用,所有WooCommerce订单时,如果购物车有"拍卖"产品在它.
我需要为购物车中的每个拍卖产品增加20的俱乐部费用,并 for each 拍卖产品额外的15%的佣金.
我的问题是:我不知道如何计算这些费用,因为佣金是根据每种产品的中标价格计算的.一张可以是100美元,另一张是50美元--以此类推.
这就是我试过的,当在一个本地主机上测试two个拍卖产品时,似乎不起作用,每个拍卖产品价值$100美元.总费用应该是$15 + $15 + $20 + $20 = $70美元,但我在 checkout 页面上什么也没有得到.完全不收费.
This is the code:个
add_action( 'woocommerce_cart_calculate_fees', 'commission_and_fees', 10, 1 );
function commission_and_fees( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) ) return;
if ( ! is_user_logged_in() ) return;
// define arguments
$clubbing_fee = '';
$percentage_fee_per_item = '';
// false, init
$is_auction = false;
// loop through and break if any auction products are found
foreach ( $cart_object->cart_contents as $item ) {
// when found, break
if ( $item['data']->is_type( 'auction' ) ) {
$is_auction = true;
break;
}
}
if ( $is_auction ) {
$percentage_fee_per_item = 15;
$clubbing_fee = 20;
$percentage_and_clubbing_fee = $cart_object->cart_contents_total * $percentage_fee_per_item / 100 + $clubbing_fee;
$cart_object->add_fee( "Auction Fees ($percentage_fee_per_item% + Clubbing Fee)", $percentage_and_clubbing_fee, true );
}
}