特殊产品的运输有折扣吗?
所有美国的运费为$ 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);
答案 0 :(得分:2)
假设您要修改woocommerce_package_rates
过滤器,则购物车中的所有项目都位于$package
键内的contents
变量中,因此您可以简单地遍历它并提取所有ID检查您想要的产品是否在那里。
目的地信息也位于$package
键内的destination
变量中,它包含完整的送货地址,包括国家/地区;
这是$package
变量的json输出示例
{
"contents": {
"044a23cadb567653eb51d4eb40acaa88": {
"key": "044a23cadb567653eb51d4eb40acaa88",
"product_id": 2754,
"variation_id": 0,
"variation": [],
"quantity": 2,
"data_hash": "b5c1d5ca8bae6d4896cf1807cdf763f0",
"line_tax_data": {
"subtotal": [],
"total": []
},
"line_subtotal": 35.9,
"line_subtotal_tax": 0,
"line_total": 35.9,
"line_tax": 0,
"data": {}
},
"0fc170ecbb8ff1afb2c6de48ea5343e7": {
"key": "0fc170ecbb8ff1afb2c6de48ea5343e7",
"product_id": 2800,
"variation_id": 0,
"variation": [],
"quantity": 1,
"data_hash": "b5c1d5ca8bae6d4896cf1807cdf763f0",
"line_tax_data": {
"subtotal": [],
"total": []
},
"line_subtotal": 107.95,
"line_subtotal_tax": 0,
"line_total": 107.95,
"line_tax": 0,
"data": {}
}
},
"contents_cost": 143.85,
"applied_coupons": [],
"user": {
"ID": 1
},
"destination": {
"country": "US",
"state": "",
"postcode": "",
"city": "Pine Bluff",
"address": "",
"address_1": "",
"address_2": ""
},
"cart_subtotal": 143.85,
"rates": {
"flat_rate:3": {}
}
}
因此,利用该信息,您可以执行类似的操作来修改“运费和运费”标签
add_filter('woocommerce_package_rates', '_shipping_cost_product_ID', 100, 2);
function _shipping_cost_product_ID( $rates, $package ){
// duh
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return $rates;
// The ID of the product you want to modify shipping cost if found in cart
$discounted_product_id = 2800;
// Get all the product IDs added in cart and put them in array
$ids_in_cart = [];
foreach ( $package['contents'] as $key => $item ) {
$ids_in_cart[] = $item['product_id'];
}
// loop through each shipping rates
foreach ( $rates as $rate_key => $rate ){
$has_taxes = false;
// Only modify shipping if all conditions below are meet
if(
'flat_rate' === $rate->method_id // if shipping method is "Flat RATE"
&& ( isset($package['destination']['country'] ) && $package['destination']['country'] == 'US' ) // if country is US
&& in_array($discounted_product_id, $ids_in_cart )//if $discounted_product_id is in cart
) {
$rates[$rate_key]->label = 'Cool Shipping'; // Modify Shipping Name
$rates[$rate_key]->cost = 3.00; // Modify Shiping Cost
//Taxes rate cost (if enabled)
$taxes = [];
foreach ($rates[$rate_key]->taxes as $key => $tax){
if( $rates[$rate_key]->taxes[$key] > 0 ){
$initial_tax_cost = $new_tax_cost = $rates[$rate_key]->taxes[$key];
$tax_rate = $initial_tax_cost / $initial_cost;
$taxes[$key] = $new_cost * $tax_rate;
$has_taxes = true;
}
}
if( $has_taxes ) $rates[$rate_key]->taxes = $taxes;
}
}
return $rates;
}