1 回答
TA贡献1909条经验 获得超7个赞
好的,我没有找到更改送货区域的方法。所以我根据支付网关和运输区域在购物车中添加了额外费用。
add_filter( 'woocommerce_before_calculate_totals', 'update_the_cart_with_extra_fee');
function update_the_cart_with_extra_fee(){
global $woocommerce;
$targeted_zones_names = array('SH1'); // Targeted zone names
$current_payment_gateway = WC()->session->get('chosen_payment_method'); // Selected payment gateway
// Current zone name
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' ); // The chosen shipping mehod
$chosen_method = explode(':', reset($chosen_methods) );
$shipping_zone = WC_Shipping_Zones::get_zone_by( 'instance_id', $chosen_method[1] );
$current_zone_name = $shipping_zone->get_zone_name();
if($current_payment_gateway === "cod" && $current_zone_name === "SH1"){
$extra_shipping_cost = 0;
//Loop through the cart to find out the extra costs
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
//Get the product info
$_product = $values['data'];
//Get the custom field value - make sure it's i.e. 10 not $10.00
$custom_shipping_cost = 60;
//Adding together the extra costs
$extra_shipping_cost = $extra_shipping_cost + $custom_shipping_cost;
$woocommerce->cart->add_fee( __('Cash on Delivery', 'woocommerce'), $extra_shipping_cost ); // Place this outside of foreach loop if you want to add fee for every product in the cart.
}
}
}
我希望这会帮助某人。
- 1 回答
- 0 关注
- 247 浏览
添加回答
举报