WooCommerce skip checkout fields if checkbox is unset










0














In WooCommerce checkout form I have a checkbox for customers who are willing to buy as a company. If the checkbox is checked - two fields reveal via js - default billing_company and custom billing_company_vat. What I'm trying to archive that if the checkbox is unchecked then these two fields won't be saved in order meta.



JS for showing/hiding checkout fields:



// checking if checkbox is checked on page load
if (!jQuery('body #checkbox_trigger').is(":checked"))
jQuery('body #billing_company_field, body #billing_company_vat_field').hide();


// show/hide on checkbox check
jQuery('body #checkbox_trigger').change(function()
if(jQuery(this).is(":checked"))
jQuery('body #billing_company_field, body #billing_company_vat_field').show();

else
jQuery('body #billing_company_field, body #billing_company_vat_field').hide();

);


PHP for deleting billing_company and billing_company_vat fields from order meta if checkbox is unchecked:



add_action( 'woocommerce_checkout_update_order_meta', 'checkout_company_fields', 10, 2 );
function checkout_company_fields( $order_id, $posted )
if (isset($posted['checkbox_trigger']) && !$posted['checkbox_trigger'])
delete_post_meta($order_id, '_billing_company');
delete_post_meta($order_id, '_shipping_company');
delete_post_meta($order_id, '_billing_company_vat');




This works and does what I need but I have a sense that this is somehow wrong and shouldn't be done this way - I guess that all checkout field data is sent to the payment gateway and is deleted from order meta only afterward when checkout is complete. I believe these two fields should be "unset" before sending the data to the payment gateway (PayPal etc.)



Also sometimes there is fatal error coming from payment gateway not sure if it's related.



[12-Nov-2018 10:49:14 UTC] PHP Fatal error: Uncaught Error: Call to a member function get_total() on boolean in /localhost/wp-content/plugins/woo-payment-gateway/includes/class-wc-gateway.php:321









share|improve this question




























    0














    In WooCommerce checkout form I have a checkbox for customers who are willing to buy as a company. If the checkbox is checked - two fields reveal via js - default billing_company and custom billing_company_vat. What I'm trying to archive that if the checkbox is unchecked then these two fields won't be saved in order meta.



    JS for showing/hiding checkout fields:



    // checking if checkbox is checked on page load
    if (!jQuery('body #checkbox_trigger').is(":checked"))
    jQuery('body #billing_company_field, body #billing_company_vat_field').hide();


    // show/hide on checkbox check
    jQuery('body #checkbox_trigger').change(function()
    if(jQuery(this).is(":checked"))
    jQuery('body #billing_company_field, body #billing_company_vat_field').show();

    else
    jQuery('body #billing_company_field, body #billing_company_vat_field').hide();

    );


    PHP for deleting billing_company and billing_company_vat fields from order meta if checkbox is unchecked:



    add_action( 'woocommerce_checkout_update_order_meta', 'checkout_company_fields', 10, 2 );
    function checkout_company_fields( $order_id, $posted )
    if (isset($posted['checkbox_trigger']) && !$posted['checkbox_trigger'])
    delete_post_meta($order_id, '_billing_company');
    delete_post_meta($order_id, '_shipping_company');
    delete_post_meta($order_id, '_billing_company_vat');




    This works and does what I need but I have a sense that this is somehow wrong and shouldn't be done this way - I guess that all checkout field data is sent to the payment gateway and is deleted from order meta only afterward when checkout is complete. I believe these two fields should be "unset" before sending the data to the payment gateway (PayPal etc.)



    Also sometimes there is fatal error coming from payment gateway not sure if it's related.



    [12-Nov-2018 10:49:14 UTC] PHP Fatal error: Uncaught Error: Call to a member function get_total() on boolean in /localhost/wp-content/plugins/woo-payment-gateway/includes/class-wc-gateway.php:321









    share|improve this question


























      0












      0








      0


      0





      In WooCommerce checkout form I have a checkbox for customers who are willing to buy as a company. If the checkbox is checked - two fields reveal via js - default billing_company and custom billing_company_vat. What I'm trying to archive that if the checkbox is unchecked then these two fields won't be saved in order meta.



      JS for showing/hiding checkout fields:



      // checking if checkbox is checked on page load
      if (!jQuery('body #checkbox_trigger').is(":checked"))
      jQuery('body #billing_company_field, body #billing_company_vat_field').hide();


      // show/hide on checkbox check
      jQuery('body #checkbox_trigger').change(function()
      if(jQuery(this).is(":checked"))
      jQuery('body #billing_company_field, body #billing_company_vat_field').show();

      else
      jQuery('body #billing_company_field, body #billing_company_vat_field').hide();

      );


      PHP for deleting billing_company and billing_company_vat fields from order meta if checkbox is unchecked:



      add_action( 'woocommerce_checkout_update_order_meta', 'checkout_company_fields', 10, 2 );
      function checkout_company_fields( $order_id, $posted )
      if (isset($posted['checkbox_trigger']) && !$posted['checkbox_trigger'])
      delete_post_meta($order_id, '_billing_company');
      delete_post_meta($order_id, '_shipping_company');
      delete_post_meta($order_id, '_billing_company_vat');




      This works and does what I need but I have a sense that this is somehow wrong and shouldn't be done this way - I guess that all checkout field data is sent to the payment gateway and is deleted from order meta only afterward when checkout is complete. I believe these two fields should be "unset" before sending the data to the payment gateway (PayPal etc.)



      Also sometimes there is fatal error coming from payment gateway not sure if it's related.



      [12-Nov-2018 10:49:14 UTC] PHP Fatal error: Uncaught Error: Call to a member function get_total() on boolean in /localhost/wp-content/plugins/woo-payment-gateway/includes/class-wc-gateway.php:321









      share|improve this question















      In WooCommerce checkout form I have a checkbox for customers who are willing to buy as a company. If the checkbox is checked - two fields reveal via js - default billing_company and custom billing_company_vat. What I'm trying to archive that if the checkbox is unchecked then these two fields won't be saved in order meta.



      JS for showing/hiding checkout fields:



      // checking if checkbox is checked on page load
      if (!jQuery('body #checkbox_trigger').is(":checked"))
      jQuery('body #billing_company_field, body #billing_company_vat_field').hide();


      // show/hide on checkbox check
      jQuery('body #checkbox_trigger').change(function()
      if(jQuery(this).is(":checked"))
      jQuery('body #billing_company_field, body #billing_company_vat_field').show();

      else
      jQuery('body #billing_company_field, body #billing_company_vat_field').hide();

      );


      PHP for deleting billing_company and billing_company_vat fields from order meta if checkbox is unchecked:



      add_action( 'woocommerce_checkout_update_order_meta', 'checkout_company_fields', 10, 2 );
      function checkout_company_fields( $order_id, $posted )
      if (isset($posted['checkbox_trigger']) && !$posted['checkbox_trigger'])
      delete_post_meta($order_id, '_billing_company');
      delete_post_meta($order_id, '_shipping_company');
      delete_post_meta($order_id, '_billing_company_vat');




      This works and does what I need but I have a sense that this is somehow wrong and shouldn't be done this way - I guess that all checkout field data is sent to the payment gateway and is deleted from order meta only afterward when checkout is complete. I believe these two fields should be "unset" before sending the data to the payment gateway (PayPal etc.)



      Also sometimes there is fatal error coming from payment gateway not sure if it's related.



      [12-Nov-2018 10:49:14 UTC] PHP Fatal error: Uncaught Error: Call to a member function get_total() on boolean in /localhost/wp-content/plugins/woo-payment-gateway/includes/class-wc-gateway.php:321






      php wordpress woocommerce






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 11:48









      Gufran Hasan

      3,42641326




      3,42641326










      asked Nov 12 at 11:47









      Vytenis

      12




      12



























          active

          oldest

          votes











          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%2f53261525%2fwoocommerce-skip-checkout-fields-if-checkbox-is-unset%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          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%2f53261525%2fwoocommerce-skip-checkout-fields-if-checkbox-is-unset%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

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          ReactJS Fetched API data displays live - need Data displayed static

          政党