我有许多航运类,价格设置在多个船运区。是否可以检测购物车中的任何产品是否属于特定的配送类,如果是,则将配送成本设置为0并显示一条消息吗?
我想使用这种机制的产品,需要特殊处理,并将通过货运。因此,如果客户的订单包含这些产品中的任何一种,装运报价将在售后手工提供。
谢谢你的帮助。
发布于 2018-06-20 01:59:57
以下代码将在购物车项中找到特定定义的配送方法并显示自定义通知时,将所有运输成本设置为零。
第二个挂钩函数是可选的,它将在签出页面中显示自定义通知。
您必须在航运选项(选项卡)下的航运设置中临时“启用调试模式”才能禁用/清除缓存。
在下面的代码中,在每个函数中定义您的发货类、段塞和您的自定义通知:
// 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文件中。应该行得通。
不要忘记禁用“调试模式”在传送设置,一旦这已经过一次测试。
发布于 2018-06-19 20:19:17
为“免费托运”设置一个“托运类”和“托运方法”,将该类分配给要为其提供免费送货的产品,然后确保在默认送货方法处设置了“免费托运”方法。
祝好运!
https://stackoverflow.com/questions/50936025
复制相似问题