首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >覆盖Woocommerce中特定运输类的所有运输成本

覆盖Woocommerce中特定运输类的所有运输成本
EN

Stack Overflow用户
提问于 2018-06-19 19:56:55
回答 2查看 3.1K关注 0票数 1

我有许多航运类,价格设置在多个船运区。是否可以检测购物车中的任何产品是否属于特定的配送类,如果是,则将配送成本设置为0并显示一条消息吗?

我想使用这种机制的产品,需要特殊处理,并将通过货运。因此,如果客户的订单包含这些产品中的任何一种,装运报价将在售后手工提供。

谢谢你的帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-20 01:59:57

以下代码将在购物车项中找到特定定义的配送方法并显示自定义通知时,将所有运输成本设置为零。

第二个挂钩函数是可选的,它将在签出页面中显示自定义通知。

您必须在航运选项(选项卡)下的航运设置中临时“启用调试模式”才能禁用/清除缓存。

在下面的代码中,在每个函数中定义您的发货类、段塞和您的自定义通知:

代码语言:javascript
复制
// 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文件中。应该行得通。

不要忘记禁用“调试模式”在传送设置,一旦这已经过一次测试。

票数 1
EN

Stack Overflow用户

发布于 2018-06-19 20:19:17

为“免费托运”设置一个“托运类”和“托运方法”,将该类分配给要为其提供免费送货的产品,然后确保在默认送货方法处设置了“免费托运”方法。

祝好运!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50936025

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档