特殊产品的运费有任何折扣吗?
所有美国运费为 10.00 美元,但如果产品在购物车中,运费将为 3.00 美元我已经使用优惠券代码尝试过。如何查看账单/发货国家/地区?
但它对总成本设置了折扣,但我需要对运费进行折扣,我也使用不同的运输类别。
我正在尝试什么。
function shipping_costs_discounted( $rates, $package ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return $rates;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product_id_in_cart = array( 1391193 );// this is product ID
if( in_array( $cart_item['product_id'], $product_id_in_cart ) ) {
//Looking for the shipping classes
$shipping_class = 'special-offer-discount'; // <=== Shipping class slug
$discount_rate = 1.46;
$is_found = false;
// Loop through cart items and checking for the specific defined shipping class
foreach( $package['contents'] as $cart_item ) {
if( $cart_item['data']->get_shipping_class() == $shipping_class )
$is_found = true;
}
// Set shipping costs shipping class is found
if( $is_found ){
foreach ( $rates as $rate_key => $rate ){
$has_taxes = false;
// Targeting "flat rate"
if( 'flat_rate' === $rate->method_id ){
$rates[$rate_key]->cost = $rate->cost - $discount_rate;
// Taxes rate cost (if enabled)
foreach ($rates[$rate_key]->taxes as $key => $tax){
if( $tax > 0 ){
$has_taxes = true;
$taxes[$key] = $tax - $discount_rate;
}
}
if( $has_taxes )
$rates[$rate_key]->taxes = $taxes;
}
}
}
return $rates;
}
}
}
add_filter('woocommerce_package_rates', 'shipping_costs_discounted', 10, 2);
慕哥6287543