覆盖 Woocommerce 中特定运费 class 的所有运费
Override all shipping costs for a specific shipping class in Woocommerce
我有许多运输 classes,其价格设置在多个运输区域。是否可以检测购物车中的任何产品是否属于特定运费 class,如果是,则将运费设置为 0 并显示一条消息?
我想对需要特殊处理并通过货运运输的产品使用此机制。因此,如果客户的订单包含任何这些产品,运费报价将手动提供 post-sale.
感谢您的帮助。
设置免费送货的送货 Class 和送货方式,将 Class 分配给您要为其提供免费送货的产品,然后确保免费送货方式设置在默认送货方式。
祝你好运!
当在购物车商品中找到特定定义的运输方式时,以下代码会将所有运费设置为零,并显示自定义通知。
第二个钩子函数是可选的,将在结帐页面显示自定义通知。
You will have to temporarily "Enable debug mode" in Shipping settings under Shipping options (tab) to disable / clear caching.
在下面的代码中,在每个函数中定义您的运输 class slug 和您的自定义通知:
// Null shipping costs for a specific shipping class and display a message
add_filter('woocommerce_package_rates', 'shipping_class_null_shipping_costs', 10, 2);
function shipping_class_null_shipping_costs( $rates, $package ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return $rates;
// HERE set your shipping class slug
$shipping_class_slug = 'extra';
$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_slug )
$found = true;
}
// Set shipping costs to zero if shipping class is found
if( $found ){
foreach ( $rates as $rate_key => $rate ){
$has_taxes = false;
// Targetting "flat rate" and local pickup
if( 'flat_rate' === $rate->method_id || 'local_pickup' === $rate->method_id ){
// Set the cost to zero
$rates[$rate_key]->cost = 0;
// Taxes rate cost (if enabled)
foreach ($rates[$rate_key]->taxes as $key => $tax){
if( $rates[$rate_key]->taxes[$key] > 0 ){
$has_taxes = true;
// Set taxes cost to zero
$taxes[$key] = 0;
}
}
if( $has_taxes )
$rates[$rate_key]->taxes = $taxes;
}
}
// Clear duplicated notices
wc_clear_notices();
// Add a custom notice to be displayed
wc_add_notice( __('My custom shipping message here.', 'woocommerce'), 'notice' );
}
return $rates;
}
add_action( 'woocommerce_before_checkout_form', 'display_custom_shipping_message_in_checkout' );
function display_custom_shipping_message_in_checkout(){
// HERE set your shipping class slug
$shipping_class_slug = 'extra';
$found = false;
// Loop through cart items and checking for the specific defined shipping class
foreach( WC()->cart->get_cart() as $cart_item ) {
if( $cart_item['data']->get_shipping_class() == $shipping_class_slug )
$found = true;
}
if( $found ){
// Display a custom notice
wc_print_notice( __('My custom shipping message here.', 'woocommerce'), 'notice' );
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。它应该有效。
Don't forget to disable "debug mode" in shipping settings once this has been tested once.
我有许多运输 classes,其价格设置在多个运输区域。是否可以检测购物车中的任何产品是否属于特定运费 class,如果是,则将运费设置为 0 并显示一条消息?
我想对需要特殊处理并通过货运运输的产品使用此机制。因此,如果客户的订单包含任何这些产品,运费报价将手动提供 post-sale.
感谢您的帮助。
设置免费送货的送货 Class 和送货方式,将 Class 分配给您要为其提供免费送货的产品,然后确保免费送货方式设置在默认送货方式。
祝你好运!
当在购物车商品中找到特定定义的运输方式时,以下代码会将所有运费设置为零,并显示自定义通知。
第二个钩子函数是可选的,将在结帐页面显示自定义通知。
You will have to temporarily "Enable debug mode" in Shipping settings under Shipping options (tab) to disable / clear caching.
在下面的代码中,在每个函数中定义您的运输 class slug 和您的自定义通知:
// Null shipping costs for a specific shipping class and display a message
add_filter('woocommerce_package_rates', 'shipping_class_null_shipping_costs', 10, 2);
function shipping_class_null_shipping_costs( $rates, $package ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return $rates;
// HERE set your shipping class slug
$shipping_class_slug = 'extra';
$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_slug )
$found = true;
}
// Set shipping costs to zero if shipping class is found
if( $found ){
foreach ( $rates as $rate_key => $rate ){
$has_taxes = false;
// Targetting "flat rate" and local pickup
if( 'flat_rate' === $rate->method_id || 'local_pickup' === $rate->method_id ){
// Set the cost to zero
$rates[$rate_key]->cost = 0;
// Taxes rate cost (if enabled)
foreach ($rates[$rate_key]->taxes as $key => $tax){
if( $rates[$rate_key]->taxes[$key] > 0 ){
$has_taxes = true;
// Set taxes cost to zero
$taxes[$key] = 0;
}
}
if( $has_taxes )
$rates[$rate_key]->taxes = $taxes;
}
}
// Clear duplicated notices
wc_clear_notices();
// Add a custom notice to be displayed
wc_add_notice( __('My custom shipping message here.', 'woocommerce'), 'notice' );
}
return $rates;
}
add_action( 'woocommerce_before_checkout_form', 'display_custom_shipping_message_in_checkout' );
function display_custom_shipping_message_in_checkout(){
// HERE set your shipping class slug
$shipping_class_slug = 'extra';
$found = false;
// Loop through cart items and checking for the specific defined shipping class
foreach( WC()->cart->get_cart() as $cart_item ) {
if( $cart_item['data']->get_shipping_class() == $shipping_class_slug )
$found = true;
}
if( $found ){
// Display a custom notice
wc_print_notice( __('My custom shipping message here.', 'woocommerce'), 'notice' );
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。它应该有效。
Don't forget to disable "debug mode" in shipping settings once this has been tested once.