【问题标题】:Woocommerce - Calcutlate Shipping cost on total amount of order, but without couponWoocommerce - 计算订单总额的运费,但没有优惠券
【发布时间】:2022-01-22 03:55:58
【问题描述】:

首先,我不是专业开发人员,对于客户,我们有以下要求。 目前,在 Woocommerce 中,运费是根据总订单金额 ([fee percent="8"]) --> 订单金额的 8% 计算的。

客户现在希望,如果使用优惠券,运费应根据原始金额而不是新的总金额与优惠券金额计算。

有什么办法可以顺利解决吗?

谢谢! 朱利安

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    您需要使用 woocommerce_package_rates 过滤器挂钩,其中获取除优惠券金额外的购物车总金额,并在循环中更改每个运费的运费。如果您遇到任何问题,请告诉我。

    add_filter( 'woocommerce_package_rates', 'custom_shipping_costs', 10, 2 );
    function custom_shipping_costs( $rates, $package ) {
        $carttotal = WC()->cart->get_cart_subtotal();
        
        $taxes = array();
        foreach ($rates[$rate_key]->taxes as $key => $tax){
            if( $rates[$rate_key]->taxes[$key] > 0 ){
                $taxes[$key] = $carttotal * 0.08;
            }
        }
        $rates[$rate_key]->taxes = $taxes;
    }
    

    【讨论】:

    • 一个好的答案将始终包括解释为什么这会解决问题,以便 OP 和任何未来的读者可以从中学习。
    • @Tyler2P 我更新了答案,如果您需要更多解释,请告诉我。
    • 您好,感谢您提供这个!不幸的是,这不起作用,在将其添加到子主题的 functions.php 后,不显示任何运输并且所有运输方式都消失了......
    【解决方案2】:

    这是我的解决方案:

    1.

    add_filter( 'woocommerce_package_rates', 'custom_shipping_costs', 20, 2 );
    function custom_shipping_costs( $rates, $package ) {
    global $woocommerce;
        
        if (!empty(WC()->cart->applied_coupons)){
        
        $totalfees = WC()->cart->get_fees();  //$order->get_total_fees();
        $producttotal = WC()->cart->subtotal; // $order->get_subtotal();
        $total = (float)$totalfees + (float)$producttotal;
        
       // New shipping cost (can be calculated)
        $new_cost = $total * .08;
        $tax_rate = array();
    
        foreach( $rates as $rate_key => $rate ){
            // Excluding free shipping methods
            if( $rate->method_id != 'free_shipping'){
    
                // Set rate cost
                $rates[$rate_key]->cost = $new_cost;
    
                // Set taxes rate cost (if enabled)
                $taxes = array();
                foreach ($rates[$rate_key]->taxes as $key => $tax){
                    if( $rates[$rate_key]->taxes[$key] > 0 )
                        $taxes[$key] = $new_cost * $tax_rate;
                }
                $rates[$rate_key]->taxes = $taxes;
    
            }
        }
        
        return $rates;
    }
    }
    
    if(!empty( $order->get_used_coupons() )) {
    
        $totalfees = $order->get_total_fees();
        $producttotal = $order->get_subtotal();
        $total = (float)$totalfees + (float)$producttotal;
        
    
    // Get the customer country code
    $country_code = $order->get_shipping_country();
    
    // Set the array for tax calculations
    $calculate_tax_for = array(
        'country' => $country_code,
        'state' => '', // Can be set (optional)
        'postcode' => '', // Can be set (optional)
        'city' => '', // Can be set (optional)
    );
    
    // Optionally, set a total shipping amount
    $new_ship_price =  $total * .08;
    
    // Get a new instance of the WC_Order_Item_Shipping Object
    $item = new WC_Order_Item_Shipping();
    
    $item->set_method_title( "Flat rate" );
    $item->set_method_id( "flat_rate:14" ); // set an existing Shipping method rate ID
    $item->set_total( $new_ship_price ); // (optional)
    $item->calculate_taxes($calculate_tax_for);
    
    $order->add_item( $item );
    
    $order->calculate_totals();
    
    $order->update_status('on-hold');
    
    // $order->save(); // If you don't update the order status
    }
    

    资源1234

    【讨论】:

      猜你喜欢
      • 2019-07-29
      • 2015-09-08
      • 2017-05-02
      • 1970-01-01
      • 2017-09-24
      • 2017-03-14
      • 1970-01-01
      • 2016-12-20
      • 2017-12-12
      相关资源
      最近更新 更多