根据 Woocommerce 中的运费 class 有条件地设置运费
Set shipping cost conditionally based on a shipping class in Woocommerce
我们在 Woocommerce 网站上销售样品产品,这只是一个可变产品。该产品具有独特的运费 class,可以以 1.99 的价格交付。
实际上,如果商品属于该唯一运费 class,则始终设置此费用,即使还有其他商品也是如此。
如果可能的话,我希望启用该运费 仅当 特定商品 (来自该唯一运费 class) 购物车中只有一个人。
感谢任何帮助。
如果具有特定运费 class 的商品与其他商品融为一体,则以下挂钩函数会将运费设置为 0:
add_filter('woocommerce_package_rates', 'conditional_shipping_class_cost', 15, 2);
function conditional_shipping_class_cost( $rates, $package ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return $rates;
// HERE define the targeted shipping method
$shipping_class = 'Extra';
// Initializing variables
$found = $others = false;
// Loop through cart items and checking for the specific product
foreach( $package['contents'] as $item ) {
if( $item['data']->get_shipping_class() == sanitize_title($shipping_class) ){
$found = true; // Has the shipping class
} else {
$others = true; // NOT the shipping class
}
}
// When items with the defined shipping are not alone in cart
if( $found && $others ){
// Loop through shipping rates
foreach ( $rates as $rate_key => $rate ){
// For Flat rate and Local pickup shipping methods
if( $rate->method_id == 'flat_rate' ) {
// Set the cost to zero
$rates[$rate_key]->cost = 0;
$rates[$rate_key]->label = 'f: '.$found.' | o: '.$others.' ';
// Initializing variables
$has_taxes = false;
$taxes = [];
// Loop through the shipping taxes array (as they can be many)
foreach ($rates[$rate_key]->taxes as $key => $tax){
if( $rates[$rate_key]->taxes[$key] > 0 ){
// Set the tax cost to zero
$taxes[$key] = 0;
$has_taxes = true;
}
}
// Set new taxes cost array
if( $has_taxes )
$rates[$rate_key]->taxes = $taxes;
}
}
}
return $rates;
}
此代码位于您的活动子主题(或主题)的 function.php 文件中。已测试并有效。
我们在 Woocommerce 网站上销售样品产品,这只是一个可变产品。该产品具有独特的运费 class,可以以 1.99 的价格交付。
实际上,如果商品属于该唯一运费 class,则始终设置此费用,即使还有其他商品也是如此。
如果可能的话,我希望启用该运费 仅当 特定商品 (来自该唯一运费 class) 购物车中只有一个人。
感谢任何帮助。
如果具有特定运费 class 的商品与其他商品融为一体,则以下挂钩函数会将运费设置为 0:
add_filter('woocommerce_package_rates', 'conditional_shipping_class_cost', 15, 2);
function conditional_shipping_class_cost( $rates, $package ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return $rates;
// HERE define the targeted shipping method
$shipping_class = 'Extra';
// Initializing variables
$found = $others = false;
// Loop through cart items and checking for the specific product
foreach( $package['contents'] as $item ) {
if( $item['data']->get_shipping_class() == sanitize_title($shipping_class) ){
$found = true; // Has the shipping class
} else {
$others = true; // NOT the shipping class
}
}
// When items with the defined shipping are not alone in cart
if( $found && $others ){
// Loop through shipping rates
foreach ( $rates as $rate_key => $rate ){
// For Flat rate and Local pickup shipping methods
if( $rate->method_id == 'flat_rate' ) {
// Set the cost to zero
$rates[$rate_key]->cost = 0;
$rates[$rate_key]->label = 'f: '.$found.' | o: '.$others.' ';
// Initializing variables
$has_taxes = false;
$taxes = [];
// Loop through the shipping taxes array (as they can be many)
foreach ($rates[$rate_key]->taxes as $key => $tax){
if( $rates[$rate_key]->taxes[$key] > 0 ){
// Set the tax cost to zero
$taxes[$key] = 0;
$has_taxes = true;
}
}
// Set new taxes cost array
if( $has_taxes )
$rates[$rate_key]->taxes = $taxes;
}
}
}
return $rates;
}
此代码位于您的活动子主题(或主题)的 function.php 文件中。已测试并有效。