Remove duplicated shipping packages using a shipping class in Woocommerce

五迷三道 提交于 2019-12-06 14:39:59

The problem here is related to splitting packages conflict between your two shipping plugins, when mixed items are in cart. In that case each plugin split the shipping package, which add 4 split packages instead of 2.

Those plugins are using woocommerce_cart_shipping_packages to split the shipping packages with an unknown priority (so I will set a very high priority).

The following code will keep the first 2 split packages from cart (and checkout too):

add_filter( 'woocommerce_cart_shipping_packages', 'remove_split_packages_based_on_items_shipping_class', 100000, 1 );
function remove_split_packages_based_on_items_shipping_class( $packages ) {
    $has_printful = $has_printify = false; // Initializing

    // Lopp through cart items
    foreach( WC()->cart->get_cart() as $item ){
        // Check items for shipping class "printful"
        if( $item['data']->get_shipping_class() === 'printful' ){
            $has_printful = true;
        } else {
            $has_printify = true;
        }
    }

    // When cart items are mixed (using both shipping plugins)
    if( $has_printful && $has_printify ){
        // Loop through split shipping packages
        foreach( $packages as $key => $package ) {
            // Keeping only the 2 first split shipping packages
            if( $key >= 2 ){
                // Removing other split shipping packages
                unset($packages[$key]);
            }
        }
    }

    return $packages;
}

Code goes in function.php file of your active child theme (active theme). It should works and display only two shipping packages when cart items are mixed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!