Remove duplicated shipping packages using a shipping class in Woocommerce










1














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…



enter image description here



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:



enter image description here



Any help is appreciated.










share|improve this question























  • you can't target shipping_method_2_printify_shipping_s as its just an html tag id… What you are targeting is printify_shipping_s or printify_shipping_e for the package ID 2. But for woocommerce_package_rates filter hook there is no way to target a specific package ID, so the code from my answer will not be useful in your case. I dont have the answer to your question and I can't help.
    – LoicTheAztec
    Nov 7 '18 at 16:36











  • @LoicTheAztec Thank you for your insight and quick response. Basically I just want to remove 'Shipping 3' and 'Shipping 4' imgur.com/a/2HZRaCw
    – user2059376
    Nov 7 '18 at 16:41
















1














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…



enter image description here



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:



enter image description here



Any help is appreciated.










share|improve this question























  • you can't target shipping_method_2_printify_shipping_s as its just an html tag id… What you are targeting is printify_shipping_s or printify_shipping_e for the package ID 2. But for woocommerce_package_rates filter hook there is no way to target a specific package ID, so the code from my answer will not be useful in your case. I dont have the answer to your question and I can't help.
    – LoicTheAztec
    Nov 7 '18 at 16:36











  • @LoicTheAztec Thank you for your insight and quick response. Basically I just want to remove 'Shipping 3' and 'Shipping 4' imgur.com/a/2HZRaCw
    – user2059376
    Nov 7 '18 at 16:41














1












1








1


1





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…



enter image description here



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:



enter image description here



Any help is appreciated.










share|improve this question















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…



enter image description here



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:



enter image description here



Any help is appreciated.







php wordpress woocommerce cart shipping-method






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 22:40









LoicTheAztec

85.2k136095




85.2k136095










asked Nov 7 '18 at 0:27









user2059376

487




487











  • you can't target shipping_method_2_printify_shipping_s as its just an html tag id… What you are targeting is printify_shipping_s or printify_shipping_e for the package ID 2. But for woocommerce_package_rates filter hook there is no way to target a specific package ID, so the code from my answer will not be useful in your case. I dont have the answer to your question and I can't help.
    – LoicTheAztec
    Nov 7 '18 at 16:36











  • @LoicTheAztec Thank you for your insight and quick response. Basically I just want to remove 'Shipping 3' and 'Shipping 4' imgur.com/a/2HZRaCw
    – user2059376
    Nov 7 '18 at 16:41

















  • you can't target shipping_method_2_printify_shipping_s as its just an html tag id… What you are targeting is printify_shipping_s or printify_shipping_e for the package ID 2. But for woocommerce_package_rates filter hook there is no way to target a specific package ID, so the code from my answer will not be useful in your case. I dont have the answer to your question and I can't help.
    – LoicTheAztec
    Nov 7 '18 at 16:36











  • @LoicTheAztec Thank you for your insight and quick response. Basically I just want to remove 'Shipping 3' and 'Shipping 4' imgur.com/a/2HZRaCw
    – user2059376
    Nov 7 '18 at 16:41
















you can't target shipping_method_2_printify_shipping_s as its just an html tag id… What you are targeting is printify_shipping_s or printify_shipping_e for the package ID 2. But for woocommerce_package_rates filter hook there is no way to target a specific package ID, so the code from my answer will not be useful in your case. I dont have the answer to your question and I can't help.
– LoicTheAztec
Nov 7 '18 at 16:36





you can't target shipping_method_2_printify_shipping_s as its just an html tag id… What you are targeting is printify_shipping_s or printify_shipping_e for the package ID 2. But for woocommerce_package_rates filter hook there is no way to target a specific package ID, so the code from my answer will not be useful in your case. I dont have the answer to your question and I can't help.
– LoicTheAztec
Nov 7 '18 at 16:36













@LoicTheAztec Thank you for your insight and quick response. Basically I just want to remove 'Shipping 3' and 'Shipping 4' imgur.com/a/2HZRaCw
– user2059376
Nov 7 '18 at 16:41





@LoicTheAztec Thank you for your insight and quick response. Basically I just want to remove 'Shipping 3' and 'Shipping 4' imgur.com/a/2HZRaCw
– user2059376
Nov 7 '18 at 16:41













1 Answer
1






active

oldest

votes


















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.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53182057%2fremove-duplicated-shipping-packages-using-a-shipping-class-in-woocommerce%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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.






    share|improve this answer



























      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.






      share|improve this answer

























        1












        1








        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.






        share|improve this answer














        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 12 '18 at 22:35

























        answered Nov 12 '18 at 22:22









        LoicTheAztec

        85.2k136095




        85.2k136095



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53182057%2fremove-duplicated-shipping-packages-using-a-shipping-class-in-woocommerce%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto