Remove duplicated shipping packages using a shipping class in Woocommerce

岁酱吖の 提交于 2019-12-08 06:18:47

问题


The products in my website are handled by one of that 2 shipping plugins: Printful Integration for WooCommerce and Printify for WooCommerce Shipping. when there is mixed items from each shipping plugin. Those plugins split each one the shipping package in two when there is mixed items (which is a a conflict and a problem).

So I have added a shipping class 'printful' (which id is 548) to the products that are handled by the Printful plugin, and tried to adjust Hide shipping method for specific shipping classes in woocommerce answer code by @LoicTheAzec (cheers), to only remove the shipping method from a specific duplicated shipping packages with ids 2 and 3 due to the conflict between the shipping plugins…

Here is my actual code:

    add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
function hide_shipping_method_based_on_shipping_class( $rates, $package )
{
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // HERE define your shipping class to find
    $class = 548; //CAMDEN HARBOR CHART MUG is in shipping class

    // HERE define the shipping methods you want to hide
    $method_key_ids = array('printify_shipping_s', 'printify_shipping_e');

    // Checking in cart items
    foreach( WC()->cart->get_cart() as $cart_item ){
        // If we find the shipping class
        if( $cart_item['data']->get_shipping_class_id() == $class ){
            foreach( $method_key_ids as $method_key_id ){

                unset($rates[$method_key_id]); // Remove the targeted methods
            }
            break; // Stop the loop
        }
    }
    return $rates;
}

But it's not working and I still get 4 shipping packages instead of two:

Any help is appreciated.


回答1:


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.



来源:https://stackoverflow.com/questions/53182057/remove-duplicated-shipping-packages-using-a-shipping-class-in-woocommerce

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