Add a custom notice for backordered cart items in Woocommerce checkout












1















I am trying to add an action that will check if there is a product in the cart on backorder and if true display a message before the checkout form. This is what i have so far but it doesn't seem to be working. Am i messing something ?



add_action( 'woocommerce_before_checkout_form', 'checkout_add_cart_notice' );
function checkout_add_cart_notice() {
$message = "Please allow 2-3 weeks for the custom order product.";

if ( check_cart_has_backorder_product() )
wc_add_notice( $message, 'error' );

}

function check_cart_has_backorder_product() {
foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
$cart_product = wc_get_product( $values['data']->get_id() );

if( $cart_product->is_on_backorder() )
return true;
}

return false;
}









share|improve this question





























    1















    I am trying to add an action that will check if there is a product in the cart on backorder and if true display a message before the checkout form. This is what i have so far but it doesn't seem to be working. Am i messing something ?



    add_action( 'woocommerce_before_checkout_form', 'checkout_add_cart_notice' );
    function checkout_add_cart_notice() {
    $message = "Please allow 2-3 weeks for the custom order product.";

    if ( check_cart_has_backorder_product() )
    wc_add_notice( $message, 'error' );

    }

    function check_cart_has_backorder_product() {
    foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
    $cart_product = wc_get_product( $values['data']->get_id() );

    if( $cart_product->is_on_backorder() )
    return true;
    }

    return false;
    }









    share|improve this question



























      1












      1








      1








      I am trying to add an action that will check if there is a product in the cart on backorder and if true display a message before the checkout form. This is what i have so far but it doesn't seem to be working. Am i messing something ?



      add_action( 'woocommerce_before_checkout_form', 'checkout_add_cart_notice' );
      function checkout_add_cart_notice() {
      $message = "Please allow 2-3 weeks for the custom order product.";

      if ( check_cart_has_backorder_product() )
      wc_add_notice( $message, 'error' );

      }

      function check_cart_has_backorder_product() {
      foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
      $cart_product = wc_get_product( $values['data']->get_id() );

      if( $cart_product->is_on_backorder() )
      return true;
      }

      return false;
      }









      share|improve this question
















      I am trying to add an action that will check if there is a product in the cart on backorder and if true display a message before the checkout form. This is what i have so far but it doesn't seem to be working. Am i messing something ?



      add_action( 'woocommerce_before_checkout_form', 'checkout_add_cart_notice' );
      function checkout_add_cart_notice() {
      $message = "Please allow 2-3 weeks for the custom order product.";

      if ( check_cart_has_backorder_product() )
      wc_add_notice( $message, 'error' );

      }

      function check_cart_has_backorder_product() {
      foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
      $cart_product = wc_get_product( $values['data']->get_id() );

      if( $cart_product->is_on_backorder() )
      return true;
      }

      return false;
      }






      php wordpress woocommerce checkout notice






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 25 '18 at 20:51









      LoicTheAztec

      90.8k1365105




      90.8k1365105










      asked Nov 25 '18 at 19:48









      alexgomyalexgomy

      11611




      11611
























          1 Answer
          1






          active

          oldest

          votes


















          1














          The following code will display a custom message on checkout page when there is backordered items in cart:



          add_action( 'woocommerce_before_checkout_form', 'backordered_items_checkout_notice' );
          function backordered_items_checkout_notice() {
          $found = false;

          foreach( WC()->cart->get_cart() as $cart_item ) {
          if( $cart_item['data']->is_on_backorder( $cart_item['quantity'] ) ) {
          $found = true;
          break;
          }
          }

          if( $found ) {
          wc_print_notice( __("Please allow 2-3 weeks for the custom order product.", "woocommerce"), 'notice' );
          }
          }


          Code goes in function.php file of your active child theme (or active theme). Tested and works.



          enter image description here






          share|improve this answer
























          • You sir are a WooCommerce master. Thanks. I adapted slightly so it also worked on the cart page. Thanks again.

            – alexgomy
            Nov 26 '18 at 15:57











          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%2f53471247%2fadd-a-custom-notice-for-backordered-cart-items-in-woocommerce-checkout%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 following code will display a custom message on checkout page when there is backordered items in cart:



          add_action( 'woocommerce_before_checkout_form', 'backordered_items_checkout_notice' );
          function backordered_items_checkout_notice() {
          $found = false;

          foreach( WC()->cart->get_cart() as $cart_item ) {
          if( $cart_item['data']->is_on_backorder( $cart_item['quantity'] ) ) {
          $found = true;
          break;
          }
          }

          if( $found ) {
          wc_print_notice( __("Please allow 2-3 weeks for the custom order product.", "woocommerce"), 'notice' );
          }
          }


          Code goes in function.php file of your active child theme (or active theme). Tested and works.



          enter image description here






          share|improve this answer
























          • You sir are a WooCommerce master. Thanks. I adapted slightly so it also worked on the cart page. Thanks again.

            – alexgomy
            Nov 26 '18 at 15:57
















          1














          The following code will display a custom message on checkout page when there is backordered items in cart:



          add_action( 'woocommerce_before_checkout_form', 'backordered_items_checkout_notice' );
          function backordered_items_checkout_notice() {
          $found = false;

          foreach( WC()->cart->get_cart() as $cart_item ) {
          if( $cart_item['data']->is_on_backorder( $cart_item['quantity'] ) ) {
          $found = true;
          break;
          }
          }

          if( $found ) {
          wc_print_notice( __("Please allow 2-3 weeks for the custom order product.", "woocommerce"), 'notice' );
          }
          }


          Code goes in function.php file of your active child theme (or active theme). Tested and works.



          enter image description here






          share|improve this answer
























          • You sir are a WooCommerce master. Thanks. I adapted slightly so it also worked on the cart page. Thanks again.

            – alexgomy
            Nov 26 '18 at 15:57














          1












          1








          1







          The following code will display a custom message on checkout page when there is backordered items in cart:



          add_action( 'woocommerce_before_checkout_form', 'backordered_items_checkout_notice' );
          function backordered_items_checkout_notice() {
          $found = false;

          foreach( WC()->cart->get_cart() as $cart_item ) {
          if( $cart_item['data']->is_on_backorder( $cart_item['quantity'] ) ) {
          $found = true;
          break;
          }
          }

          if( $found ) {
          wc_print_notice( __("Please allow 2-3 weeks for the custom order product.", "woocommerce"), 'notice' );
          }
          }


          Code goes in function.php file of your active child theme (or active theme). Tested and works.



          enter image description here






          share|improve this answer













          The following code will display a custom message on checkout page when there is backordered items in cart:



          add_action( 'woocommerce_before_checkout_form', 'backordered_items_checkout_notice' );
          function backordered_items_checkout_notice() {
          $found = false;

          foreach( WC()->cart->get_cart() as $cart_item ) {
          if( $cart_item['data']->is_on_backorder( $cart_item['quantity'] ) ) {
          $found = true;
          break;
          }
          }

          if( $found ) {
          wc_print_notice( __("Please allow 2-3 weeks for the custom order product.", "woocommerce"), 'notice' );
          }
          }


          Code goes in function.php file of your active child theme (or active theme). Tested and works.



          enter image description here







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 25 '18 at 20:49









          LoicTheAztecLoicTheAztec

          90.8k1365105




          90.8k1365105













          • You sir are a WooCommerce master. Thanks. I adapted slightly so it also worked on the cart page. Thanks again.

            – alexgomy
            Nov 26 '18 at 15:57



















          • You sir are a WooCommerce master. Thanks. I adapted slightly so it also worked on the cart page. Thanks again.

            – alexgomy
            Nov 26 '18 at 15:57

















          You sir are a WooCommerce master. Thanks. I adapted slightly so it also worked on the cart page. Thanks again.

          – alexgomy
          Nov 26 '18 at 15:57





          You sir are a WooCommerce master. Thanks. I adapted slightly so it also worked on the cart page. Thanks again.

          – alexgomy
          Nov 26 '18 at 15:57




















          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%2f53471247%2fadd-a-custom-notice-for-backordered-cart-items-in-woocommerce-checkout%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

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'