Woocommerce - 根据重量仅向某些类别(或除某些类别外的所有购物车)添加购物车附加费
Woocommerce - add surcharge in cart based on weight only to some categories (or to all cart except some categories)
朋友让我在购物车上按重量加额外费用,并且只针对特定类别(或排除某些类别,这无关紧要)。
主题是,对于夏天,他想在包装中加冰以保持产品(例如牛奶、奶酪等)冷藏。
他还销售小工具和带导游参观他的工厂等,所以他不想对这些产品收取额外费用。
基于“”的回答,我下面的代码版本,对整个购物车收取额外费用,从该费用中排除访问产品,因为访问的权重显然为 0。
但我不是代码专家,我不知道如何插入数组以包含 'milk' 和 'cheese' 等类别(反之亦然,以排除 'visits' 和'gadgets').
在上瘾中,我的代码以 3 千克为步长增加费用(由于数据包大小 DHL/UPS/GLS 等)
/* Extra Fee based on weight */
add_action( 'woocommerce_cart_calculate_fees', 'shipping_weight_fee', 30, 1 );
function shipping_weight_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Convert in grams
$cart_weight = $cart->get_cart_contents_weight() * 1000;
$fee = 0.00; // initial fee
// if cart is > 0 add €1,20 to initial fee by steps of 3000g
if( $cart_weight > 0 ){
for( $i = 0; $i < $cart_weight; $i += 3000 ){
$fee += 1.20;
}
}
// add the fee / doesn't show extra fee if it's 0
if ( !empty( $fee )) {
$cart->add_fee( __( 'Extra for ice' ), $fee, false );
}
}
最后一个问题是:为什么 $i 变量可以是 0...1...1000000 而结果没有任何变化?代码似乎完全一样...
谢谢
以下代码基于:
- 预定义类别
- 基于产品重量(属于预定义类别的产品)
- 费用逐步增加
(注释在代码中添加说明)
function shipping_weight_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
/* SETTINGS */
// Specific categories
$specific_categories = array( 'categorie-1', 'categorie-2' );
// Initial fee
$fee = 1.20;
// Steps of kg
$steps_of_kg = 3;
/* END SETTINGS */
// Set variable
$total_weight = 0;
// Loop though each cart item
foreach ( $cart->get_cart() as $cart_item ) {
// Get product id
$product_id = $cart_item['product_id'];
// Get weight
$product_weight = $cart_item['data']->get_weight();
// NOT empty & has certain category
if ( ! empty( $product_weight ) && has_term( $specific_categories, 'product_cat', $product_id ) ) {
// Quantity
$product_quantity = $cart_item['quantity'];
// Add to total
$total_weight += $product_weight * $product_quantity;
}
}
if ( $total_weight > 0 ) {
$increase_by_steps = ceil( $total_weight / $steps_of_kg );
// Add fee
$cart->add_fee( __( 'Extra for ice' ), $fee * $increase_by_steps, false );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'shipping_weight_fee', 10, 1 );
朋友让我在购物车上按重量加额外费用,并且只针对特定类别(或排除某些类别,这无关紧要)。
主题是,对于夏天,他想在包装中加冰以保持产品(例如牛奶、奶酪等)冷藏。
他还销售小工具和带导游参观他的工厂等,所以他不想对这些产品收取额外费用。
基于“
但我不是代码专家,我不知道如何插入数组以包含 'milk' 和 'cheese' 等类别(反之亦然,以排除 'visits' 和'gadgets').
在上瘾中,我的代码以 3 千克为步长增加费用(由于数据包大小 DHL/UPS/GLS 等)
/* Extra Fee based on weight */
add_action( 'woocommerce_cart_calculate_fees', 'shipping_weight_fee', 30, 1 );
function shipping_weight_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Convert in grams
$cart_weight = $cart->get_cart_contents_weight() * 1000;
$fee = 0.00; // initial fee
// if cart is > 0 add €1,20 to initial fee by steps of 3000g
if( $cart_weight > 0 ){
for( $i = 0; $i < $cart_weight; $i += 3000 ){
$fee += 1.20;
}
}
// add the fee / doesn't show extra fee if it's 0
if ( !empty( $fee )) {
$cart->add_fee( __( 'Extra for ice' ), $fee, false );
}
}
最后一个问题是:为什么 $i 变量可以是 0...1...1000000 而结果没有任何变化?代码似乎完全一样...
谢谢
以下代码基于:
- 预定义类别
- 基于产品重量(属于预定义类别的产品)
- 费用逐步增加
(注释在代码中添加说明)
function shipping_weight_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
/* SETTINGS */
// Specific categories
$specific_categories = array( 'categorie-1', 'categorie-2' );
// Initial fee
$fee = 1.20;
// Steps of kg
$steps_of_kg = 3;
/* END SETTINGS */
// Set variable
$total_weight = 0;
// Loop though each cart item
foreach ( $cart->get_cart() as $cart_item ) {
// Get product id
$product_id = $cart_item['product_id'];
// Get weight
$product_weight = $cart_item['data']->get_weight();
// NOT empty & has certain category
if ( ! empty( $product_weight ) && has_term( $specific_categories, 'product_cat', $product_id ) ) {
// Quantity
$product_quantity = $cart_item['quantity'];
// Add to total
$total_weight += $product_weight * $product_quantity;
}
}
if ( $total_weight > 0 ) {
$increase_by_steps = ceil( $total_weight / $steps_of_kg );
// Add fee
$cart->add_fee( __( 'Extra for ice' ), $fee * $increase_by_steps, false );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'shipping_weight_fee', 10, 1 );