根据 Woocommerce 中的购物车商品尺寸添加费用
Add a fee based on cart items dimensions in Woocommerce
我在 2 月在这里得到了很大的帮助,关于一个 woocommerce 问题,我需要根据购物车总高度自动添加费用,。
现在,我还需要在代码中添加对项目宽度的计算。但前提是购物车中的任何物品的宽度超过 25 厘米(不是总宽度,因为产品是堆叠在一起的书籍,所以额外的运费是根据总高度和宽度超过 25 厘米计算的) .
示例:
实际起作用的是什么:
- 如果手推车的总高度为 3 厘米或以上,将收取费用。
需要什么(补充):
- 如果一件商品的宽度超过 25 厘米,则需要支付费用。
- 如果购物车的总高度为 3 厘米或以上,并且如果一件商品的宽度超过 25 厘米,则需要支付费用。
我一直在研究 "" 答案代码(第二个代码片段),试图向它添加一个高度变量($target_width = 25; // ),但我在计算中迷失了方向,不知道如何在它不变成购物车总宽度的情况下进行尝试,而且我只是没有资格进行如此高级的代码编辑。我所有的不同尝试都没有奏效。
感谢我能得到的任何帮助。
已更新:以下代码将处理您的附加商品的要求,如果商品宽度超过 25 厘米,也会收取费用:
add_action( 'woocommerce_cart_calculate_fees', 'shipping_dimensions_fee', 10, 1 );
function shipping_dimensions_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Your settings (here below)
$target_height = 3; // The defined height in cm (equal or over)
$width_threshold = 25; // The defined item with to set the fee.
$fee = 50; // The fee amount
// Initializing variables
$total_height = 0;
$apply_fee = false;
// Loop through cart items
foreach( $cart->get_cart() as $cart_item ){
// Calculating total height
$total_height += $cart_item['data']->get_height() * $cart_item['quantity'];
// Checking item with
if ( $cart_item['data']->get_width() > $width_threshold ) {
$apply_fee = true;
}
}
// Add the fee
if( $total_height >= $target_height || $apply_fee ) {
$cart->add_fee( __( 'Dimensions shipping fee' ), $fee, false );
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。它应该有效。
我在 2 月在这里得到了很大的帮助,关于一个 woocommerce 问题,我需要根据购物车总高度自动添加费用,
现在,我还需要在代码中添加对项目宽度的计算。但前提是购物车中的任何物品的宽度超过 25 厘米(不是总宽度,因为产品是堆叠在一起的书籍,所以额外的运费是根据总高度和宽度超过 25 厘米计算的) . 示例:
实际起作用的是什么:
- 如果手推车的总高度为 3 厘米或以上,将收取费用。
需要什么(补充):
- 如果一件商品的宽度超过 25 厘米,则需要支付费用。
- 如果购物车的总高度为 3 厘米或以上,并且如果一件商品的宽度超过 25 厘米,则需要支付费用。
我一直在研究 "
感谢我能得到的任何帮助。
已更新:以下代码将处理您的附加商品的要求,如果商品宽度超过 25 厘米,也会收取费用:
add_action( 'woocommerce_cart_calculate_fees', 'shipping_dimensions_fee', 10, 1 );
function shipping_dimensions_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Your settings (here below)
$target_height = 3; // The defined height in cm (equal or over)
$width_threshold = 25; // The defined item with to set the fee.
$fee = 50; // The fee amount
// Initializing variables
$total_height = 0;
$apply_fee = false;
// Loop through cart items
foreach( $cart->get_cart() as $cart_item ){
// Calculating total height
$total_height += $cart_item['data']->get_height() * $cart_item['quantity'];
// Checking item with
if ( $cart_item['data']->get_width() > $width_threshold ) {
$apply_fee = true;
}
}
// Add the fee
if( $total_height >= $target_height || $apply_fee ) {
$cart->add_fee( __( 'Dimensions shipping fee' ), $fee, false );
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。它应该有效。