我正在寻找一种方法,使客户降低运费,基于购物车的总百分比,统一费率和数量的每种产品.
6种产品的:统一运费20欧元
12种产品的:统一运费30欧元
这是如何做到的呢?
发布于 2017-10-17 03:24:45
不需要插件…就可以做到这一点。
1)您首先需要在每个船运区的WooCommerce运输设置中为“统一费率”方法设置一定数量的WooCommerce:

此金额将在我们的函数中更改为20€,从1项更改为11项,对于12项或更多项更改为30€。这一数额将减少10%的购物车总数。
2)然后使用woocommerce_package_rates过滤器钩子中的自定义函数,可以根据购物车项目计数和购物车总数对运费进行折扣。
这是代码:
add_filter( 'woocommerce_package_rates', 'custom_package_rates', 10, 2 );
function custom_package_rates( $rates, $packages ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
// Get some cart data and set variable values
$cart_count = WC()->cart->get_cart_contents_count();
$cart_total = WC()->cart->cart_contents_total;
$cart_10_percent = $cart_total * 0.1;
$flat_rate_value = 20; // Default "Flat rate" value
foreach($rates as $rate_key => $rate_values ) {
$method_id = $rate_values->method_id;
$rate_id = $rate_values->id;
if( $method_id == 'flat_rate' ){
if( $cart_count < 6 )
$cart_10_percent = 0; // No percent discount
elseif( $cart_count >= 12 )
$flat_rate_value = 30; // "Flat rate" value for 12 or more items
$rate_cost = $flat_rate_value > $cart_10_percent ? $flat_rate_value - $cart_10_percent : 0;
// Set the new calculated rate cost
$rates[$rate_id]->cost = number_format( $rates[$rate_id]->cost * $rate_cost, 2 );
// Taxes rate cost (if enabled)
$taxes = array();
foreach ($rates[$rate_id]->taxes as $key => $tax){
if( $tax > 0 ){ // set the new tax cost
// set the discounted tax cost
$taxes[$key] = number_format( $tax * $rate_cost, 2 );
}
}
$rates[$rate_id]->taxes = $taxes;
}
}
return $rates;
} 代码在您的活动子主题(或主题)的function.php文件中,或者在任何插件文件中。
在WooCommerce 3上测试并工作。
sometimes) 刷新运输缓存(所需的) 首先,清空你的手推车。 2)该代码已经保存在您的function.php文件中。 3)进入运输区域设置,禁用一个“统一费率”(例如)和“保存”。然后重新启用“统一利率”和“储蓄”。,你完成了,你可以测试它了。
https://stackoverflow.com/questions/46780166
复制相似问题