代码之家  ›  专栏  ›  技术社区  ›  Louis W

覆盖Woomerce中特定运输类别的所有运输成本

  •  1
  • Louis W  · 技术社区  · 8 年前

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

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

    谢谢你的帮助。

    2 回复  |  直到 8 年前
        1
  •  1
  •   LoicTheAztec    8 年前

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

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

    你将不得不暂时 启用调试模式 “在“装运选项”下的“装运设置”中 (选项卡) .

    在下面的代码中,在每个函数中定义shipping类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文件中。它应该有用。

    别忘了 禁用“调试模式” 在运输设置中,一旦测试一次。

        2
  •  0
  •   Nathan Diehl    8 年前

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

    祝你好运!