1

我在二月份得到了很大的帮助,关于 woocommerce 问题,我需要根据购物车的总高度自动添加费用,在这个线程中。

现在,我还需要在代码中添加对项目宽度的计算。但前提是购物车中的任何物品的宽度超过 25 厘米(不是总宽度,因为产品是堆叠在一起的书籍,所以额外的运费是根据总高度和宽度超过 25 厘米计算的) . 例子:

什么是实际工作:

  • 如果购物车的总高度为 3 厘米或以上,则需要付费。

需要什么(另外):

  • 如果一件物品的宽度超过 25 厘米,则需要付费。
  • 如果购物车的总高度为 3 厘米或以上,并且如果一件物品的宽度超过 25 厘米,则需要付费。

我一直在玩根据 Woocommerce 中的购物车项目高度添加费用答案代码(第二个代码片段),试图向它添加一个高度变量($target_width = 25; // ),但我迷路了计算,不知道如何在不变成总购物车宽度的情况下尝试它,我只是没有资格对代码进行如此高级的编辑。我所有的不同尝试都没有奏效。

感谢我能得到的任何帮助。

4

1 回答 1

1

更新:以下代码将处理您有要求的附加物品,如果物品宽度超过 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 文件中。它应该有效。

于 2019-09-16T13:48:02.217 回答