增加 WooCommerce 购物车中每个类别计数的运费
Increase Shipping cost per categories count found in WooCommerce cart
我正在尝试根据购物车中的类别数量(计数)增加运费。到目前为止我有这个:
add_filter( 'woocommerce_package_rates', 'vh_wc_shipping_costs', 20 );
function vh_wc_shipping_costs( $rates ) {
$increase_cost = 50;
$cat_ids = array();
$count = 0;
foreach ( wc()->cart->get_cart() as $cart_item_key => $cart_item ) {
$cat_ids = array_merge(
$cat_ids,
(array) $cart_item['data']->get_category_ids()
);
$count = count( $cat_ids );
}
$i=0;
if ( $count >= 1 ) {
foreach ( $rates as $rate_key => $rate ) {
$i++; $i <= $count;
// Excluding free shipping methods
if ( 'free_shipping' !== $rate->method_id ) {
// Get old cost
$old_cost = $rates[ $rate_key ]->cost;
// Set rate cost
$rates[ $rate_key ]->cost = $old_cost + $increase_cost;
}
}
}
return $rates;
}
但出于某种原因,它不会增加每个额外类别的运费。另外,我想对额外费用进行说明。任何建议表示赞赏。
由于当时Whosebug上的规则是一个问题,所以我只回答与您的代码相关的第一个问题。
以下代码将为在购物车中找到的每个额外产品类别添加额外的运费(因此不是第一个):
add_filter( 'woocommerce_package_rates', 'filter_shipping_rates_costs', 10, 2 );
function filter_shipping_rates_costs( $rates, $package ) {
$step_cost = 50;
$term_ids = array();
// Loop through cart items for the current shipping package
foreach( $package['contents'] as $cart_item ){
$term_ids = array_merge(
$term_ids,
(array) $cart_item['data']->get_category_ids()
);
}
$terms_count = count( $term_ids );
// Loop through shipping rates
foreach ( $rates as $rate_key => $rate ) {
// Excluding free shipping methods
if ( 'free_shipping' !== $rate->method_id && $terms_count > 1 ) {
// Set rate cost
$rates[$rate_key]->cost = $rate->cost + ($step_cost * ($terms_count - 1));
}
}
return $rates;
}
现在,如果您想为购物车中的每个类别添加额外的运费,请使用以下方法:
add_filter( 'woocommerce_package_rates', 'filter_shipping_rates_costs', 10, 2 );
function filter_shipping_rates_costs( $rates, $package ) {
$step_cost = 50;
$term_ids = array();
// Loop through cart items for the current shipping package
foreach( $package['contents'] as $cart_item ){
$term_ids = array_merge(
$term_ids,
(array) $cart_item['data']->get_category_ids()
);
}
$terms_count = count( $term_ids );
// Loop through shipping rates
foreach ( $rates as $rate_key => $rate ) {
// Excluding free shipping methods
if ( 'free_shipping' !== $rate->method_id && $terms_count > 0 ) {
// Set rate cost
$rates[$rate_key]->cost = $rate->cost + ($step_cost * $terms_count);
}
}
return $rates;
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
Refresh the shipping caches:
- This code is already saved on your functions.php file.
- In a shipping zone settings, disable / save any shipping method, then enable back / save.
You are done and you can test it.
我正在尝试根据购物车中的类别数量(计数)增加运费。到目前为止我有这个:
add_filter( 'woocommerce_package_rates', 'vh_wc_shipping_costs', 20 );
function vh_wc_shipping_costs( $rates ) {
$increase_cost = 50;
$cat_ids = array();
$count = 0;
foreach ( wc()->cart->get_cart() as $cart_item_key => $cart_item ) {
$cat_ids = array_merge(
$cat_ids,
(array) $cart_item['data']->get_category_ids()
);
$count = count( $cat_ids );
}
$i=0;
if ( $count >= 1 ) {
foreach ( $rates as $rate_key => $rate ) {
$i++; $i <= $count;
// Excluding free shipping methods
if ( 'free_shipping' !== $rate->method_id ) {
// Get old cost
$old_cost = $rates[ $rate_key ]->cost;
// Set rate cost
$rates[ $rate_key ]->cost = $old_cost + $increase_cost;
}
}
}
return $rates;
}
但出于某种原因,它不会增加每个额外类别的运费。另外,我想对额外费用进行说明。任何建议表示赞赏。
由于当时Whosebug上的规则是一个问题,所以我只回答与您的代码相关的第一个问题。
以下代码将为在购物车中找到的每个额外产品类别添加额外的运费(因此不是第一个):
add_filter( 'woocommerce_package_rates', 'filter_shipping_rates_costs', 10, 2 );
function filter_shipping_rates_costs( $rates, $package ) {
$step_cost = 50;
$term_ids = array();
// Loop through cart items for the current shipping package
foreach( $package['contents'] as $cart_item ){
$term_ids = array_merge(
$term_ids,
(array) $cart_item['data']->get_category_ids()
);
}
$terms_count = count( $term_ids );
// Loop through shipping rates
foreach ( $rates as $rate_key => $rate ) {
// Excluding free shipping methods
if ( 'free_shipping' !== $rate->method_id && $terms_count > 1 ) {
// Set rate cost
$rates[$rate_key]->cost = $rate->cost + ($step_cost * ($terms_count - 1));
}
}
return $rates;
}
现在,如果您想为购物车中的每个类别添加额外的运费,请使用以下方法:
add_filter( 'woocommerce_package_rates', 'filter_shipping_rates_costs', 10, 2 );
function filter_shipping_rates_costs( $rates, $package ) {
$step_cost = 50;
$term_ids = array();
// Loop through cart items for the current shipping package
foreach( $package['contents'] as $cart_item ){
$term_ids = array_merge(
$term_ids,
(array) $cart_item['data']->get_category_ids()
);
}
$terms_count = count( $term_ids );
// Loop through shipping rates
foreach ( $rates as $rate_key => $rate ) {
// Excluding free shipping methods
if ( 'free_shipping' !== $rate->method_id && $terms_count > 0 ) {
// Set rate cost
$rates[$rate_key]->cost = $rate->cost + ($step_cost * $terms_count);
}
}
return $rates;
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
Refresh the shipping caches:
- This code is already saved on your functions.php file.
- In a shipping zone settings, disable / save any shipping method, then enable back / save.
You are done and you can test it.