根据 Woocommerce 中的购物车商品尺寸添加费用
·
问题:根据 Woocommerce 中的购物车商品尺寸添加费用
我在 2 月份得到了很大的帮助,关于 woocommerce 问题,我需要在此线程](https://stackoverflow.com/questions/54871678/add-a-fee-based-on-cart-items-height-in-woocommerce/54871889#54871889)中根据购物车总高度[添加自动费用。
现在,我还需要在代码中添加对项目宽度的计算。但前提是购物车中的任何物品的宽度超过 25 厘米(不是总宽度,因为产品是堆叠在一起的书籍,所以额外的运费是根据总高度和宽度超过 25 厘米计算的) .例子:
什么是实际工作:
- 如果推车总高度为 3 厘米或以上,则收取费用。
需要什么(另外):
-
如果一件物品的宽度超过 25 厘米,则需要付费。
-
如果购物车的总高度为 3 厘米或以上,并且如果一件物品的宽度超过 25 厘米,则收取费用。
我一直在玩 "根据 Woocommerce 中的购物车项目高度添加费用" 答案代码(第二个代码片段),试图为其添加一个高度变量($target_width u003d 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 文件中。它应该有效。
更多推荐
所有评论(0)