问题描述
我正在尝试根据购物车中的类别数量(计数)增加运费.到目前为止,我有这个:
I'm trying to increase shipping cost based on number of categories in cart (count). So far I have this:
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;
}
但由于某种原因,它不会增加每个额外类别的运费.另外,我想在额外费用中添加一个注释.任何建议表示赞赏.
But it doesn't increment the shipping cost for every extra category for some reason. Also, I would like to add a note to the extra cost. Any Suggestion is appreciated.
推荐答案
由于 StackOverFlow 上的规则当时是一个问题,我只会回答与您的代码相关的第一个问题.
As the rule on StackOverFlow is one question at the time, I will only answer the first question related to your code.
以下代码将为购物车中的每个附加产品类别添加额外的运费(因此不是第一个):
The following code will add an extra shipping cost for each additional product category found in cart (so not for the first one):
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;
}
现在,如果您想为购物车中的每个类别添加额外的运费,请使用以下内容:
Now if you want to add an extra shipping cost for each category found in cart, use the following:
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 文件中.经测试有效.
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
刷新运输缓存:
- 此代码已保存在您的functions.php 文件中.
- 在送货区域设置中,禁用/保存任何送货方式,然后启用返回/保存.
大功告成,您可以对其进行测试.
- 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.
这篇关于增加 WooCommerce 购物车中每个类别计数的运费的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!