首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Woocommerce -按订单总额计算的计算器运输成本,但没有优惠券。

Woocommerce -按订单总额计算的计算器运输成本,但没有优惠券。
EN

Stack Overflow用户
提问于 2021-12-20 14:34:12
回答 2查看 279关注 0票数 -1

首先,我不是一个专业的开发人员,对于一个客户,我们有以下要求。目前,在Woocommerce,运输成本是根据订单总额(费用percent="8") -订单金额的8%以上计算的。

客户现在想要的是,如果使用优惠券,运输成本应根据原始金额计算,而不是按新的优惠券金额计算。

有办法顺利解决吗?

谢谢!朱利安

EN

回答 2

Stack Overflow用户

发布于 2021-12-20 15:08:14

您需要使用woocommerce_package_rates过滤器钩子,其中得到的总购物车数量,除了优惠券金额和改变每一个运费循环的shiiping成本。如果你有什么问题告诉我。

代码语言:javascript
复制
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;
}
票数 0
EN

Stack Overflow用户

发布于 2021-12-22 20:45:57

以下是我的解决方案:

1.

代码语言:javascript
复制
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;
}
}
代码语言:javascript
复制
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
}

资源123.4.

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

https://stackoverflow.com/questions/70423402

复制
相关文章

相似问题

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