Progressive cart item custom shipping cost in Woocommerce












1















I need to figure out a way to do woocommerce shipping rates based on items on cart.
I need to charge 120 if buying 1-2 items and 180 buying 3. I added a free shipping option for 4+ (based on $)



I tried adding this to the flat rate price: 120+60([qty]-2) it works in all instances but the 1 item, because it charges $60.



Any thoughts?










share|improve this question





























    1















    I need to figure out a way to do woocommerce shipping rates based on items on cart.
    I need to charge 120 if buying 1-2 items and 180 buying 3. I added a free shipping option for 4+ (based on $)



    I tried adding this to the flat rate price: 120+60([qty]-2) it works in all instances but the 1 item, because it charges $60.



    Any thoughts?










    share|improve this question



























      1












      1








      1








      I need to figure out a way to do woocommerce shipping rates based on items on cart.
      I need to charge 120 if buying 1-2 items and 180 buying 3. I added a free shipping option for 4+ (based on $)



      I tried adding this to the flat rate price: 120+60([qty]-2) it works in all instances but the 1 item, because it charges $60.



      Any thoughts?










      share|improve this question
















      I need to figure out a way to do woocommerce shipping rates based on items on cart.
      I need to charge 120 if buying 1-2 items and 180 buying 3. I added a free shipping option for 4+ (based on $)



      I tried adding this to the flat rate price: 120+60([qty]-2) it works in all instances but the 1 item, because it charges $60.



      Any thoughts?







      php wordpress woocommerce product-quantity shipping-method






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 17:47









      LoicTheAztec

      85.9k136197




      85.9k136197










      asked Nov 13 '18 at 16:31









      Cynthia LaraCynthia Lara

      51113




      51113
























          1 Answer
          1






          active

          oldest

          votes


















          1














          With the following code, you will be able to get this shipping rates:

          - 1 or 2 items: $120

          - 3 items: $180

          - 4 items or more: Free shipping (hiding flat rate method)



          1) Add the following code to function.php file of your active child theme (active theme):



          add_filter('woocommerce_package_rates', 'custom_progressive_shipping_costs', 10, 2);
          function custom_progressive_shipping_costs( $rates, $package ){

          $items_count = WC()->cart->get_cart_contents_count();

          if( $items_count < 3 ){
          $cost_rate = 2;
          } else {
          $cost_rate = $items_count;
          }

          foreach ( $rates as $rate_key => $rate ){
          $taxes = ;
          $has_taxes = false;
          // Targeting "flat rate"
          if ( 'flat_rate' === $rate->method_id ) {
          // For 1, 2 or 3 items
          if ( $items_count <= 3 ) {
          $rates[$rate_key]->cost = $rate->cost * $cost_rate;

          // Taxes rate cost (if enabled)
          foreach ($rates[$rate_key]->taxes as $key => $tax){
          if( $tax > 0 ){
          $has_taxes = true;
          $taxes[$key] = $tax * $cost_rate;
          }
          }
          if( $has_taxes )
          $rates[$rate_key]->taxes = $taxes;
          }
          // For more than 3 hide Flat rate
          else {
          // remove flat rate method
          unset($rates[$rate_key]);
          }
          }
          }
          return $rates;
          }


          And save…



          2) In Your shipping method settings, you will need to set 60 as your "Flat rate" cost and SAVE.



          You need to keep your minimal amount for "Free shipping" method.



          You are done. Tested and works.






          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%2f53285497%2fprogressive-cart-item-custom-shipping-cost-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














            With the following code, you will be able to get this shipping rates:

            - 1 or 2 items: $120

            - 3 items: $180

            - 4 items or more: Free shipping (hiding flat rate method)



            1) Add the following code to function.php file of your active child theme (active theme):



            add_filter('woocommerce_package_rates', 'custom_progressive_shipping_costs', 10, 2);
            function custom_progressive_shipping_costs( $rates, $package ){

            $items_count = WC()->cart->get_cart_contents_count();

            if( $items_count < 3 ){
            $cost_rate = 2;
            } else {
            $cost_rate = $items_count;
            }

            foreach ( $rates as $rate_key => $rate ){
            $taxes = ;
            $has_taxes = false;
            // Targeting "flat rate"
            if ( 'flat_rate' === $rate->method_id ) {
            // For 1, 2 or 3 items
            if ( $items_count <= 3 ) {
            $rates[$rate_key]->cost = $rate->cost * $cost_rate;

            // Taxes rate cost (if enabled)
            foreach ($rates[$rate_key]->taxes as $key => $tax){
            if( $tax > 0 ){
            $has_taxes = true;
            $taxes[$key] = $tax * $cost_rate;
            }
            }
            if( $has_taxes )
            $rates[$rate_key]->taxes = $taxes;
            }
            // For more than 3 hide Flat rate
            else {
            // remove flat rate method
            unset($rates[$rate_key]);
            }
            }
            }
            return $rates;
            }


            And save…



            2) In Your shipping method settings, you will need to set 60 as your "Flat rate" cost and SAVE.



            You need to keep your minimal amount for "Free shipping" method.



            You are done. Tested and works.






            share|improve this answer






























              1














              With the following code, you will be able to get this shipping rates:

              - 1 or 2 items: $120

              - 3 items: $180

              - 4 items or more: Free shipping (hiding flat rate method)



              1) Add the following code to function.php file of your active child theme (active theme):



              add_filter('woocommerce_package_rates', 'custom_progressive_shipping_costs', 10, 2);
              function custom_progressive_shipping_costs( $rates, $package ){

              $items_count = WC()->cart->get_cart_contents_count();

              if( $items_count < 3 ){
              $cost_rate = 2;
              } else {
              $cost_rate = $items_count;
              }

              foreach ( $rates as $rate_key => $rate ){
              $taxes = ;
              $has_taxes = false;
              // Targeting "flat rate"
              if ( 'flat_rate' === $rate->method_id ) {
              // For 1, 2 or 3 items
              if ( $items_count <= 3 ) {
              $rates[$rate_key]->cost = $rate->cost * $cost_rate;

              // Taxes rate cost (if enabled)
              foreach ($rates[$rate_key]->taxes as $key => $tax){
              if( $tax > 0 ){
              $has_taxes = true;
              $taxes[$key] = $tax * $cost_rate;
              }
              }
              if( $has_taxes )
              $rates[$rate_key]->taxes = $taxes;
              }
              // For more than 3 hide Flat rate
              else {
              // remove flat rate method
              unset($rates[$rate_key]);
              }
              }
              }
              return $rates;
              }


              And save…



              2) In Your shipping method settings, you will need to set 60 as your "Flat rate" cost and SAVE.



              You need to keep your minimal amount for "Free shipping" method.



              You are done. Tested and works.






              share|improve this answer




























                1












                1








                1







                With the following code, you will be able to get this shipping rates:

                - 1 or 2 items: $120

                - 3 items: $180

                - 4 items or more: Free shipping (hiding flat rate method)



                1) Add the following code to function.php file of your active child theme (active theme):



                add_filter('woocommerce_package_rates', 'custom_progressive_shipping_costs', 10, 2);
                function custom_progressive_shipping_costs( $rates, $package ){

                $items_count = WC()->cart->get_cart_contents_count();

                if( $items_count < 3 ){
                $cost_rate = 2;
                } else {
                $cost_rate = $items_count;
                }

                foreach ( $rates as $rate_key => $rate ){
                $taxes = ;
                $has_taxes = false;
                // Targeting "flat rate"
                if ( 'flat_rate' === $rate->method_id ) {
                // For 1, 2 or 3 items
                if ( $items_count <= 3 ) {
                $rates[$rate_key]->cost = $rate->cost * $cost_rate;

                // Taxes rate cost (if enabled)
                foreach ($rates[$rate_key]->taxes as $key => $tax){
                if( $tax > 0 ){
                $has_taxes = true;
                $taxes[$key] = $tax * $cost_rate;
                }
                }
                if( $has_taxes )
                $rates[$rate_key]->taxes = $taxes;
                }
                // For more than 3 hide Flat rate
                else {
                // remove flat rate method
                unset($rates[$rate_key]);
                }
                }
                }
                return $rates;
                }


                And save…



                2) In Your shipping method settings, you will need to set 60 as your "Flat rate" cost and SAVE.



                You need to keep your minimal amount for "Free shipping" method.



                You are done. Tested and works.






                share|improve this answer















                With the following code, you will be able to get this shipping rates:

                - 1 or 2 items: $120

                - 3 items: $180

                - 4 items or more: Free shipping (hiding flat rate method)



                1) Add the following code to function.php file of your active child theme (active theme):



                add_filter('woocommerce_package_rates', 'custom_progressive_shipping_costs', 10, 2);
                function custom_progressive_shipping_costs( $rates, $package ){

                $items_count = WC()->cart->get_cart_contents_count();

                if( $items_count < 3 ){
                $cost_rate = 2;
                } else {
                $cost_rate = $items_count;
                }

                foreach ( $rates as $rate_key => $rate ){
                $taxes = ;
                $has_taxes = false;
                // Targeting "flat rate"
                if ( 'flat_rate' === $rate->method_id ) {
                // For 1, 2 or 3 items
                if ( $items_count <= 3 ) {
                $rates[$rate_key]->cost = $rate->cost * $cost_rate;

                // Taxes rate cost (if enabled)
                foreach ($rates[$rate_key]->taxes as $key => $tax){
                if( $tax > 0 ){
                $has_taxes = true;
                $taxes[$key] = $tax * $cost_rate;
                }
                }
                if( $has_taxes )
                $rates[$rate_key]->taxes = $taxes;
                }
                // For more than 3 hide Flat rate
                else {
                // remove flat rate method
                unset($rates[$rate_key]);
                }
                }
                }
                return $rates;
                }


                And save…



                2) In Your shipping method settings, you will need to set 60 as your "Flat rate" cost and SAVE.



                You need to keep your minimal amount for "Free shipping" method.



                You are done. Tested and works.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Dec 1 '18 at 6:56

























                answered Nov 13 '18 at 17:46









                LoicTheAztecLoicTheAztec

                85.9k136197




                85.9k136197






























                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285497%2fprogressive-cart-item-custom-shipping-cost-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







                    Popular posts from this blog

                    Xamarin.iOS Cant Deploy on Iphone

                    Glorious Revolution

                    Dulmage-Mendelsohn matrix decomposition in Python