根据特定运输类别增加 WooCommerce 购物车总重量

问题描述 投票:0回答:1

我的functions.php 文件中有一些自定义代码,如果结账时购物车中存在多个运输类别为343 的产品,则可以将运输类别从343 更改为344。但是,基于相同的条件,我还需要将重量(以克为单位)添加到总订单中。

即,当结帐时购物车中只有 1 种 343 类产品时,我想在订单总重量中添加 14 克。在任何其他情况下,即如果购物车中至少有 1 个运输类别为 344 的产品,或者购物车中有超过 1 个运输类别为 343 的产品,我想在订单总重量中添加 54 克。

目前,如果结帐时购物车中只有一种产品运输类别 343,则计算为属于该运输类别。但是,如果结账时购物车中有两种运输类别的产品,它们将更改为运输类别 344。并且我想根据相同的条件将上述重量添加到订单中。

//This turns 2 letter-box items into 1 standard package
add_action( 'woocommerce_before_calculate_totals', 'adjust_shipping_class_based_on_quantity' );

function adjust_shipping_class_based_on_quantity( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return; // Exit

    if ( ! $cart->needs_shipping() ) 
        return; // Exit

    $count = 0; // Initialize

    // Count items from a specific shipping class
    foreach( $cart->get_cart() as $item ) {
        if ( $item['data']->get_shipping_class_id() === 343 ) {
            // Increment count for each item with shipping class ID 343
            $count += $item['quantity'];
        }
    }

    // Check that there are at least 2 items from the specific shipping class
    if ( $count < 2 ) 
        return; // Exit
    
    // Loop through cart items to change the shipping class of items with the targeted shipping class
    foreach( $cart->get_cart() as $item ) {
        if ( $item['data']->get_shipping_class_id() === 343 ) {
            $item['data']->set_shipping_class_id( 344 ); // Change the shipping class
        }
    }
}

虽然我还没有达到可以测试任何以运输类别为条件的代码的程度,但我确实尝试看看下面的代码是否可以增加总订单的重量,但它反而导致了错误。

add_action( 'woocommerce_cart_calculate_fees', 'add_extra_weight_to_order', 10, 1 );

function add_extra_weight_to_order( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
        return;
    }

    $extra_weight = 14; 

    $total_weight = $cart->get_cart_contents_weight();
    
    $total_weight += $extra_weight;

    $cart->set_cart_contents_weight( $total_weight );
}

我应该如何解决我的问题?

php woocommerce hook-woocommerce cart checkout
1个回答
0
投票

以下内容基于特定购物车商品运输类别 ID 数量计数:

  • 将更改此购物车商品的运输类别 ID,
  • 将添加到购物车的总重量:
    • 14 克,如果运输类别数量等于 1
    • 52 克,如果运输类别数量大于 1
// Utility function: Count items in cart from a specific shipping class Id
function get_cart_shipping_class_count( $targeted_class_id, $cart = null ) {
    if ( ! $cart ) {
        $cart = WC()->cart;
    }
    $count = 0; // Initialize

    foreach( $cart->get_cart() as $item ) {
        $product = wc_get_product($item['variation_id'] > 0 ? $item['variation_id'] : $item['product_id']);
        if ( $product->get_shipping_class_id() === $targeted_class_id ) {
            $count += $item['quantity']; 
        }
    }
    return $count;
}

// Increase cart content weight conditionally
add_filter( 'woocommerce_cart_contents_weight', 'increase_conditionally_cart_contents_weight' );
function increase_conditionally_cart_contents_weight( $weight ) {
    $count  = get_cart_shipping_class_count( 343 );

    if ( $count === 1 ) {
        $weight += 14;
    } elseif ( $count > 1 ) {
        $weight += 52;
    }
    return $weight;
}

// Change conditionally cart item shipping class
add_action( 'woocommerce_before_calculate_totals', 'adjust_shipping_class_based_on_quantity' );
function adjust_shipping_class_based_on_quantity( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return; // Exit

    if ( ! $cart->needs_shipping() ) 
        return; // Exit

    $count  = get_cart_shipping_class_count( 343, $cart );

    // Check that there are at least 2 items from the specific shipping class
    if ( $count < 2 ) 
        return; // Exit
    
    // Loop through cart items to change the shipping class of items with the targeted shipping class
    foreach( $cart->get_cart() as $item ) {
        if ( $item['data']->get_shipping_class_id() === 343 ) {
            $item['data']->set_shipping_class_id( 344 ); // Change the shipping class
        }
    }
}

代码位于子主题的functions.php 文件中(或插件中)。应该可以。

相关:WooCommerce:有条件地更改特定区域的运输类别

© www.soinside.com 2019 - 2024. All rights reserved.