基于购物车商品数量的 WooCommerce 运费
WooCommerce shipping cost based on cart item count
我想根据添加到购物车中的产品数量来计算运费,例如,
如果我购买一部手机,则运费为 2.5,购买两两部以上手机后,运费为 5.0
<?php
$qty(1) * 2.5 = 2.5
$qty(2) * 2.5 = 5.0
$qty(3) * 2.5 = 5.0
?>
那么有什么想法或建议如何根据产品数量计算运费吗?
您可以添加自定义费用:添加到主题 functions.php 或使用插件
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$price_per_mobile = 2.5;
$shipcharge = ( $woocommerce->cart->cart_contents_total * $price_per_mobile);
$woocommerce->cart->add_fee( 'Total Shipping Cost', $shipcharge, true, '' );
}
Updated:
As your question is a bit unclear, you could just need to add [qty]*2.5
in the Flat rate shipping method cost (for each shipping zone) in your wooCommerce shipping settings.
But it will not work if you have 2 different items in cart like: item1 (qty 1) + item2 (qty 1)
所以这个答案在所有情况下都适用:
1) 首先,您需要为每个运输区域设置 "Flat rate" 运输方式,成本将设置为 2.5
(在您的 WooCommerce 运输设置中)。
2) 添加此代码将为每个购物车商品(基于商品总数)计算新的更新运费:
add_filter( 'woocommerce_package_rates', 'custom_flat_rate_cost_calculation', 10, 2 );
function custom_flat_rate_cost_calculation( $rates, $package )
{
// The cart count (total items in cart)
$cart_count = WC()->cart->get_cart_contents_count();
$taxes = array();
// If there is more than 1 cart item
if( $cart_count > 1 ){
// Iterating through each shipping rate
foreach($rates as $rate_key => $rate_values){
// Targeting "Flat Rate" shipping method
if ( 'flat_rate' === $rate_values->method_id ) {
// Set the new calculated rate cost
$rates[$rate_id]->cost = number_format($rates[$rate_id]->cost * $cumulated_active_quantity, 2);
// Taxes rate cost (if enabled)
foreach ($rates[$rate_id]->taxes as $key => $tax){
if( $rates[$rate_id]->taxes[$key] > 0 ){ // set the new tax cost
$taxes[$key] = number_format( $rates[$rate_id]->taxes[$key] * $cumulated_active_quantity, 2 );
$has_taxes = true;
} else {
$has_taxes = false;
}
}
if( $has_taxes )
$rates[$rate_id]->taxes = $taxes;
}
}
}
return $rates;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
此代码已在 WooCommerce 3+ 上测试并有效
You will need to refresh shipping zones caches: disabling the Flat rate, then save. And enabling back this rate and save.
继续使用 Woocommerce 过滤器 woocommerce_package_rates
。您可以在此处自定义购物车页面中可用的所有运费。
这是为国内和国际运输的所有物品增加额外费用的代码
add_filter('woocommerce_package_rates', 'wf_modify_rate', 10, 3);
function wf_modify_rate( $available_shipping_methods, $package ){
$origin_country = 'US';
$amount_to_add_domestic = 10;
$amount_to_add_inter_national = 20;
$amount_to_add = ($package['destination']['country'] == $origin_country) ?
$amount_to_add_domestic : $amount_to_add_inter_national;
$item_count = 0;
foreach ($package['contents'] as $key => $item) {
$item_count += $item['quantity'];
}
foreach ($available_shipping_methods as $methord_name => $methord) {
$available_shipping_methods[$methord_name]->cost += ($amount_to_add*$item_count);
}
return $available_shipping_methods;
}
我想根据添加到购物车中的产品数量来计算运费,例如,
如果我购买一部手机,则运费为 2.5,购买两两部以上手机后,运费为 5.0
<?php
$qty(1) * 2.5 = 2.5
$qty(2) * 2.5 = 5.0
$qty(3) * 2.5 = 5.0
?>
那么有什么想法或建议如何根据产品数量计算运费吗?
您可以添加自定义费用:添加到主题 functions.php 或使用插件
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$price_per_mobile = 2.5;
$shipcharge = ( $woocommerce->cart->cart_contents_total * $price_per_mobile);
$woocommerce->cart->add_fee( 'Total Shipping Cost', $shipcharge, true, '' );
}
Updated:
As your question is a bit unclear, you could just need to add
[qty]*2.5
in the Flat rate shipping method cost (for each shipping zone) in your wooCommerce shipping settings.But it will not work if you have 2 different items in cart like:
item1 (qty 1) + item2 (qty 1)
所以这个答案在所有情况下都适用:
1) 首先,您需要为每个运输区域设置 "Flat rate" 运输方式,成本将设置为 2.5
(在您的 WooCommerce 运输设置中)。
2) 添加此代码将为每个购物车商品(基于商品总数)计算新的更新运费:
add_filter( 'woocommerce_package_rates', 'custom_flat_rate_cost_calculation', 10, 2 );
function custom_flat_rate_cost_calculation( $rates, $package )
{
// The cart count (total items in cart)
$cart_count = WC()->cart->get_cart_contents_count();
$taxes = array();
// If there is more than 1 cart item
if( $cart_count > 1 ){
// Iterating through each shipping rate
foreach($rates as $rate_key => $rate_values){
// Targeting "Flat Rate" shipping method
if ( 'flat_rate' === $rate_values->method_id ) {
// Set the new calculated rate cost
$rates[$rate_id]->cost = number_format($rates[$rate_id]->cost * $cumulated_active_quantity, 2);
// Taxes rate cost (if enabled)
foreach ($rates[$rate_id]->taxes as $key => $tax){
if( $rates[$rate_id]->taxes[$key] > 0 ){ // set the new tax cost
$taxes[$key] = number_format( $rates[$rate_id]->taxes[$key] * $cumulated_active_quantity, 2 );
$has_taxes = true;
} else {
$has_taxes = false;
}
}
if( $has_taxes )
$rates[$rate_id]->taxes = $taxes;
}
}
}
return $rates;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
此代码已在 WooCommerce 3+ 上测试并有效
You will need to refresh shipping zones caches: disabling the Flat rate, then save. And enabling back this rate and save.
继续使用 Woocommerce 过滤器 woocommerce_package_rates
。您可以在此处自定义购物车页面中可用的所有运费。
这是为国内和国际运输的所有物品增加额外费用的代码
add_filter('woocommerce_package_rates', 'wf_modify_rate', 10, 3);
function wf_modify_rate( $available_shipping_methods, $package ){
$origin_country = 'US';
$amount_to_add_domestic = 10;
$amount_to_add_inter_national = 20;
$amount_to_add = ($package['destination']['country'] == $origin_country) ?
$amount_to_add_domestic : $amount_to_add_inter_national;
$item_count = 0;
foreach ($package['contents'] as $key => $item) {
$item_count += $item['quantity'];
}
foreach ($available_shipping_methods as $methord_name => $methord) {
$available_shipping_methods[$methord_name]->cost += ($amount_to_add*$item_count);
}
return $available_shipping_methods;
}