我正在尝试根据购物车中的类别数量(计数)来增加运费。到目前为止我有这个:
add_filter( 'woocommerce_package_rates', 'vh_wc_shipping_costs', 20 );
function vh_wc_shipping_costs( $rates ) {
$increase_cost = 50;
$cat_ids = array();
$count = 0;
foreach ( wc()->cart->get_cart() as $cart_item_key => $cart_item ) {
$cat_ids = array_merge(
$cat_ids,
(array) $cart_item['data']->get_category_ids()
);
$count = count( $cat_ids );
}
$i=0;
if ( $count >= 1 ) {
foreach ( $rates as $rate_key => $rate ) {
$i++; $i <= $count;
// Excluding free shipping methods
if ( 'free_shipping' !== $rate->method_id ) {
// Get old cost
$old_cost = $rates[ $rate_key ]->cost;
// Set rate cost
$rates[ $rate_key ]->cost = $old_cost + $increase_cost;
}
}
}
return $rates;
}
但由于某种原因,它不会增加每个额外类别的运输成本。另外,我想补充一下额外费用。任何建议表示赞赏。
吃鸡游戏