我对此代码片段有问题.我使用的是最新版本的WooCommerce(8.6.1).

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

在 checkout 期间,当客户在特定的发货区(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' );

我将上面的代码片断添加到了函数.php文件中,并添加到了FluentSnipets插件中.都没有给我带来想要的结果.我还判断了以下不兼容性,这似乎不是问题:

  • 插件不兼容
  • 与其他代码片段不兼容
  • 主题不兼容

推荐答案

您的代码中有一些错误,并且您没有使用右挂钩. try 以下操作:

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() === 29 ) {
        $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 '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() === 343 ) {
                $item['data']->set_shipping_class_id(344); // Change the shipping class
            }
        }
    }
}

代码放在子主题的functions.php文件中(或插件中).测试和作品.

Php相关问答推荐

Symfony Monolog:使用多个格式化程序

为什么注册SIGHUP的处理程序函数会阻止在PHP CLI中等待输入时点击X关闭Xterm窗口?""

在WooCommerce中处理客户总支出中的退款

PHP cUrl扩展与v8.2.12更新损坏

在Woocommerce变量产品中的标签附近显示所选属性选项名称

根据在WooCommerce购物车和 checkout 中 Select 的送货方式显示快捷代码内容

在PHP中循环访问多维数组,并按组显示内容

PHP Laravel Web应用启动时间&>15秒

如何在UML类图中可视化变量引用数组

如何用Ubuntu 22.04在VSCode中启用XDebug

用自定义代码替换 WooCommerce 单一产品简短描述

在 Heroku 上部署 Sylius - 路由不正确

调度程序不发送任何消息

CodeIgniter 4中嵌套过滤器组不起作用

自定义主键值未正确显示

PHP header() 是否缓存重定向,如果是,如何防止它这样做?

带有 nginx 的 Symfony 不提供 .js 和 .css 文件

hasOne/belongsToMany 或多对多

如何将在同一台服务器上运行的 2 个 PHP 应用程序与使用公共会话分开

将参数传递给 laravel 9 工厂