Add a Contact Form to “Out of Stock” product Variations in WooCommerce












1














I have successfully added a contact form to a product if it is Out of Stock using



add_action('woocommerce_single_product_summary', 'add_contact_form', 30,2);

function add_contact_form() {
global $product;
if(!$product->is_in_stock( )) {
echo do_shortcode('[contact-form-7 id="6513" title="Out of Stock Form"]');
}
}


But I also want to add the same form if a products variation is Out of Stock too.
Just unsure as to which hook I can use to tie it in to the Out of Stock message for that variation...
I feel like it may be something to do with line 20 of woocommerce/single-product/add-to-cart/variation.php



<div class="woocommerce-variation-availability">{{{ data.variation.availability_html }}}</div>


But am getting a bit lost TBH.



I can change the text that is diaplyed for both simple products and those with variations by using



add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);

function wcs_custom_get_availability( $availability, $_product ) {

if ( ! $_product->is_in_stock() ) {
$availability['availability'] = __('<h4 style="color:#F00;">Coming Soon</h4>', 'woocommerce');
}

return $availability;
}


But if I try and add the Form Shortcode or other HTML, like an iFrame, it is stripped out and does not render ?!










share|improve this question





























    1














    I have successfully added a contact form to a product if it is Out of Stock using



    add_action('woocommerce_single_product_summary', 'add_contact_form', 30,2);

    function add_contact_form() {
    global $product;
    if(!$product->is_in_stock( )) {
    echo do_shortcode('[contact-form-7 id="6513" title="Out of Stock Form"]');
    }
    }


    But I also want to add the same form if a products variation is Out of Stock too.
    Just unsure as to which hook I can use to tie it in to the Out of Stock message for that variation...
    I feel like it may be something to do with line 20 of woocommerce/single-product/add-to-cart/variation.php



    <div class="woocommerce-variation-availability">{{{ data.variation.availability_html }}}</div>


    But am getting a bit lost TBH.



    I can change the text that is diaplyed for both simple products and those with variations by using



    add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);

    function wcs_custom_get_availability( $availability, $_product ) {

    if ( ! $_product->is_in_stock() ) {
    $availability['availability'] = __('<h4 style="color:#F00;">Coming Soon</h4>', 'woocommerce');
    }

    return $availability;
    }


    But if I try and add the Form Shortcode or other HTML, like an iFrame, it is stripped out and does not render ?!










    share|improve this question



























      1












      1








      1







      I have successfully added a contact form to a product if it is Out of Stock using



      add_action('woocommerce_single_product_summary', 'add_contact_form', 30,2);

      function add_contact_form() {
      global $product;
      if(!$product->is_in_stock( )) {
      echo do_shortcode('[contact-form-7 id="6513" title="Out of Stock Form"]');
      }
      }


      But I also want to add the same form if a products variation is Out of Stock too.
      Just unsure as to which hook I can use to tie it in to the Out of Stock message for that variation...
      I feel like it may be something to do with line 20 of woocommerce/single-product/add-to-cart/variation.php



      <div class="woocommerce-variation-availability">{{{ data.variation.availability_html }}}</div>


      But am getting a bit lost TBH.



      I can change the text that is diaplyed for both simple products and those with variations by using



      add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);

      function wcs_custom_get_availability( $availability, $_product ) {

      if ( ! $_product->is_in_stock() ) {
      $availability['availability'] = __('<h4 style="color:#F00;">Coming Soon</h4>', 'woocommerce');
      }

      return $availability;
      }


      But if I try and add the Form Shortcode or other HTML, like an iFrame, it is stripped out and does not render ?!










      share|improve this question















      I have successfully added a contact form to a product if it is Out of Stock using



      add_action('woocommerce_single_product_summary', 'add_contact_form', 30,2);

      function add_contact_form() {
      global $product;
      if(!$product->is_in_stock( )) {
      echo do_shortcode('[contact-form-7 id="6513" title="Out of Stock Form"]');
      }
      }


      But I also want to add the same form if a products variation is Out of Stock too.
      Just unsure as to which hook I can use to tie it in to the Out of Stock message for that variation...
      I feel like it may be something to do with line 20 of woocommerce/single-product/add-to-cart/variation.php



      <div class="woocommerce-variation-availability">{{{ data.variation.availability_html }}}</div>


      But am getting a bit lost TBH.



      I can change the text that is diaplyed for both simple products and those with variations by using



      add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);

      function wcs_custom_get_availability( $availability, $_product ) {

      if ( ! $_product->is_in_stock() ) {
      $availability['availability'] = __('<h4 style="color:#F00;">Coming Soon</h4>', 'woocommerce');
      }

      return $availability;
      }


      But if I try and add the Form Shortcode or other HTML, like an iFrame, it is stripped out and does not render ?!







      php wordpress woocommerce product variations






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 at 21:09









      LoicTheAztec

      84k126095




      84k126095










      asked Nov 22 at 20:22









      Andy Ward

      204




      204
























          1 Answer
          1






          active

          oldest

          votes


















          1














          The following code will add a contact form to a selected out of stock product variation:



          add_filter( 'woocommerce_available_variation', 'form_to_out_of_stock_product_variations', 10, 3 );
          function form_to_out_of_stock_product_variations( $data, $product, $variation ) {
          if( ! $data['is_in_stock'] )
          $data['availability_html'] .= do_shortcode('[contact-form-7 id="6513" title="Out of Stock Form"]');

          return $data;
          }


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



          enter image description here





          So you will need to tweak your existing code as follow:



          add_action('woocommerce_single_product_summary', 'add_contact_form', 30,2);
          function add_contact_form() {
          global $product;

          if( ! $product->is_in_stock( ) && ! $product->is_type('variable') )
          echo do_shortcode('[contact-form-7 id="6513" title="Out of Stock Form"]');
          }


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






          share|improve this answer























          • Amazing - cheers @LoicTheAztec that worked perfectly
            – Andy Ward
            Nov 22 at 22:48











          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%2f53437611%2fadd-a-contact-form-to-out-of-stock-product-variations-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 following code will add a contact form to a selected out of stock product variation:



          add_filter( 'woocommerce_available_variation', 'form_to_out_of_stock_product_variations', 10, 3 );
          function form_to_out_of_stock_product_variations( $data, $product, $variation ) {
          if( ! $data['is_in_stock'] )
          $data['availability_html'] .= do_shortcode('[contact-form-7 id="6513" title="Out of Stock Form"]');

          return $data;
          }


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



          enter image description here





          So you will need to tweak your existing code as follow:



          add_action('woocommerce_single_product_summary', 'add_contact_form', 30,2);
          function add_contact_form() {
          global $product;

          if( ! $product->is_in_stock( ) && ! $product->is_type('variable') )
          echo do_shortcode('[contact-form-7 id="6513" title="Out of Stock Form"]');
          }


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






          share|improve this answer























          • Amazing - cheers @LoicTheAztec that worked perfectly
            – Andy Ward
            Nov 22 at 22:48
















          1














          The following code will add a contact form to a selected out of stock product variation:



          add_filter( 'woocommerce_available_variation', 'form_to_out_of_stock_product_variations', 10, 3 );
          function form_to_out_of_stock_product_variations( $data, $product, $variation ) {
          if( ! $data['is_in_stock'] )
          $data['availability_html'] .= do_shortcode('[contact-form-7 id="6513" title="Out of Stock Form"]');

          return $data;
          }


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



          enter image description here





          So you will need to tweak your existing code as follow:



          add_action('woocommerce_single_product_summary', 'add_contact_form', 30,2);
          function add_contact_form() {
          global $product;

          if( ! $product->is_in_stock( ) && ! $product->is_type('variable') )
          echo do_shortcode('[contact-form-7 id="6513" title="Out of Stock Form"]');
          }


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






          share|improve this answer























          • Amazing - cheers @LoicTheAztec that worked perfectly
            – Andy Ward
            Nov 22 at 22:48














          1












          1








          1






          The following code will add a contact form to a selected out of stock product variation:



          add_filter( 'woocommerce_available_variation', 'form_to_out_of_stock_product_variations', 10, 3 );
          function form_to_out_of_stock_product_variations( $data, $product, $variation ) {
          if( ! $data['is_in_stock'] )
          $data['availability_html'] .= do_shortcode('[contact-form-7 id="6513" title="Out of Stock Form"]');

          return $data;
          }


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



          enter image description here





          So you will need to tweak your existing code as follow:



          add_action('woocommerce_single_product_summary', 'add_contact_form', 30,2);
          function add_contact_form() {
          global $product;

          if( ! $product->is_in_stock( ) && ! $product->is_type('variable') )
          echo do_shortcode('[contact-form-7 id="6513" title="Out of Stock Form"]');
          }


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






          share|improve this answer














          The following code will add a contact form to a selected out of stock product variation:



          add_filter( 'woocommerce_available_variation', 'form_to_out_of_stock_product_variations', 10, 3 );
          function form_to_out_of_stock_product_variations( $data, $product, $variation ) {
          if( ! $data['is_in_stock'] )
          $data['availability_html'] .= do_shortcode('[contact-form-7 id="6513" title="Out of Stock Form"]');

          return $data;
          }


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



          enter image description here





          So you will need to tweak your existing code as follow:



          add_action('woocommerce_single_product_summary', 'add_contact_form', 30,2);
          function add_contact_form() {
          global $product;

          if( ! $product->is_in_stock( ) && ! $product->is_type('variable') )
          echo do_shortcode('[contact-form-7 id="6513" title="Out of Stock Form"]');
          }


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







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 at 21:14

























          answered Nov 22 at 21:09









          LoicTheAztec

          84k126095




          84k126095












          • Amazing - cheers @LoicTheAztec that worked perfectly
            – Andy Ward
            Nov 22 at 22:48


















          • Amazing - cheers @LoicTheAztec that worked perfectly
            – Andy Ward
            Nov 22 at 22:48
















          Amazing - cheers @LoicTheAztec that worked perfectly
          – Andy Ward
          Nov 22 at 22:48




          Amazing - cheers @LoicTheAztec that worked perfectly
          – Andy Ward
          Nov 22 at 22:48


















          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%2f53437611%2fadd-a-contact-form-to-out-of-stock-product-variations-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

          Contact image not getting when fetch all contact list from iPhone by CNContact

          count number of partitions of a set with n elements into k subsets

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks