WooCommerce:当购物车中有超过 1 个运输类别时,将运输类别更改为另一个运输类别

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

我对这段代码片段有疑问。我使用的是最新版本的 WooCommerce (8.6.1)。

我正在努力实现以下目标:

结账时,当客户位于特定运输区域 (ID 29),并且购物车中至少有 2 件具有特定运输类别 (ID 343) 的产品时,我希望将其运输类别更改为另一个 (ID 344)。然而,下面的代码似乎没有做任何事情。可能是什么问题?

// Custom function to change shipping class if conditions are met
function adjust_shipping_class_based_on_quantity() {
    // Get the current cart contents
    $cart = WC()->cart->get_cart();

    // Check if the cart contains items and the first item has a destination
    if ( ! empty( $cart ) && isset( $cart[0]['destination'] ) && is_array( $cart[0]['destination'] ) ) {
        // Get the shipping zone ID
        $zone_id = WC_Shipping_Zones::get_zone_matching_package( array( 'contents' => $cart ) )->get_id();

        // Check if the shipping zone ID is 29
        if ( $zone_id == 29 ) {
            // Initialize variables to count 'Letter box' items
            $letter_box_count = 0;

            // Loop through cart items to count 'Letter box' items
            foreach( $cart as $item ) {
                if ( isset( $item['data'] ) && $item['data']->get_shipping_class_id() == 343 ) {
                    // Increment count for each 'Letter box' item
                    $letter_box_count += $item['quantity'];
                }
            }

            // If there are at least 2 'Letter box' items, change shipping class of all 'Letter box' items to 344
            if ( $letter_box_count >= 2 ) {
                // Loop through cart items again to change shipping class if shipping class ID is 343
                foreach( $cart as $key => $item ) {
                    if ( isset( $item['data'] ) && $item['data']->get_shipping_class_id() == 343 ) {
                        // Change the shipping class of the item to 'regular-package'
                        $cart[$key]['data']->set_shipping_class_id(344);
                    }
                }
            }

            // Set the modified cart contents
            WC()->cart->set_cart( $cart );
        }
    }
}
add_action( 'woocommerce_cart_loaded_from_session', 'adjust_shipping_class_based_on_quantity' );

我将上述代码片段添加到我的functions.php 文件和FluentSnippets 插件中。都没有给我带来想要的结果。我还检查了以下不兼容性,这似乎不是问题:

  • 插件不兼容
  • 与其他代码片段不兼容
  • 主题不兼容
php wordpress woocommerce hook-woocommerce
1个回答
0
投票

您的代码中有一些错误,并且您没有使用正确的钩子。 尝试以下操作:

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

    // Get Shipping Packages and the Shipping Zone ID
    $shipping_packages = $cart->get_shipping_packages();
    $shipping_zone     = WC_Shipping_Zones::get_zone_matching_package( current($shipping_packages) );

    if ( empty( $shipping_zone ) ) 
        return; // Exit

    // Check if the shipping zone ID is 29
    if ( $shipping_zone->get_id() === 2 ) {
        $count = 0; // Initialize

        // Count items from a specific shipping class
        foreach( $cart->get_cart() as $item ) {
            if ( $item['data']->get_shipping_class_id() === 34 ) {
                // Increment count for each 'Letter box' item
                $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() === 34 ) {
                $item['data']->set_shipping_class_id(33); // Change the shipping class
            }
        }
    }
}

代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。

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