问题描述
我有多个运输类别,价格在多个运输区域中设定。是否可以检测出购物车中的任何产品是否属于特定的运输类别,并且将运输成本设置为0并显示一条消息?
I have a number of shipping classes with prices set in multiple shipping zones. Is it possible to detect if ANY product within the cart belongs to a specific shipping class, and if so set the shipping cost to 0 and display a message?
我想要对需要特殊处理且将通过货运运输的产品使用此机制。因此,如果客户的订单中包含上述任何产品,则将在售后手动提供运输报价。
I would like to use this mechanism for products which require special handling and would be shipped via freight. So if a customer's order contains any of these products the shipping quote would be provided manually post-sale.
感谢您的帮助。
推荐答案
当在购物车中找到特定的定义运输方式时,以下代码会将所有运输成本设置为零,并显示自定义通知。
The following code will set all shipping cost to zero, when a specific defined shipping method is found in cart items and will display a custom notice.
第二个挂钩函数是可选的,它将在结帐页面上显示自定义通知。
The second hooked function is optional and will display the custom notice in checkout page.
在下面的代码中,请在每个函数中定义您的装运类别的子弹和您的自定义通知:
In the code bellow define in each function your shipping class slug and your custom notice:
// 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文件主题)。
Code goes in function.php file of your active child theme (or active theme). It should works.
这篇关于覆盖Woocommerce中特定运输类别的所有运输成本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!