Disabling the purchase for one product in woocommerce












0















I am trying to turn off the possibility of buying a selected product, but all the materials I have found so far do not work for me. I would like to exclude a specific product, preferably by removing the button, because I want this product to work like any other, but without the possibility to be buying.



This code would be perfect for me:



add_filter('woocommerce_is_purchasable', 'filter_is_purchasable');
function filter_is_purchasable( $is_purchasable, $product ) {
return ( $product->id == 534 ? false : $is_purchasable );
}


Unfortunately it does not work. Any ideas?










share|improve this question





























    0















    I am trying to turn off the possibility of buying a selected product, but all the materials I have found so far do not work for me. I would like to exclude a specific product, preferably by removing the button, because I want this product to work like any other, but without the possibility to be buying.



    This code would be perfect for me:



    add_filter('woocommerce_is_purchasable', 'filter_is_purchasable');
    function filter_is_purchasable( $is_purchasable, $product ) {
    return ( $product->id == 534 ? false : $is_purchasable );
    }


    Unfortunately it does not work. Any ideas?










    share|improve this question



























      0












      0








      0








      I am trying to turn off the possibility of buying a selected product, but all the materials I have found so far do not work for me. I would like to exclude a specific product, preferably by removing the button, because I want this product to work like any other, but without the possibility to be buying.



      This code would be perfect for me:



      add_filter('woocommerce_is_purchasable', 'filter_is_purchasable');
      function filter_is_purchasable( $is_purchasable, $product ) {
      return ( $product->id == 534 ? false : $is_purchasable );
      }


      Unfortunately it does not work. Any ideas?










      share|improve this question
















      I am trying to turn off the possibility of buying a selected product, but all the materials I have found so far do not work for me. I would like to exclude a specific product, preferably by removing the button, because I want this product to work like any other, but without the possibility to be buying.



      This code would be perfect for me:



      add_filter('woocommerce_is_purchasable', 'filter_is_purchasable');
      function filter_is_purchasable( $is_purchasable, $product ) {
      return ( $product->id == 534 ? false : $is_purchasable );
      }


      Unfortunately it does not work. Any ideas?







      php wordpress woocommerce product






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 24 '18 at 3:22









      LoicTheAztec

      90.6k1365104




      90.6k1365104










      asked Nov 21 '18 at 0:58









      dezajnerdezajner

      82




      82
























          1 Answer
          1






          active

          oldest

          votes


















          0














          This code is just a bit outdated since Woocommerce 3, as $product->id need to be replaced with $product->get_id()… Try the following instead (where 534 is the related product ID where add to cart button will not appear):



          add_filter('woocommerce_is_purchasable', 'purchasable_product_customizations', 10, 2 );
          function purchasable_product_customizations( $is_purchasable, $product ) {
          if( in_array( $product->get_id() == 534 )
          $is_purchasable = false;

          return $is_purchasable;
          }


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





          If your product ID is a variable product



          For a variable product ID where you want to disable allvariations, you will replace this line:



          if( in_array( $product->get_id() == 534 )


          by this one:



          if( in_array( $product->get_parent_id() == 534 )



          If it still doesn't work is that something else is interacting and making trouble, like some customizations made by you, your theme or a plugin… Also it can be because you are not adding this code in the right place.







          share|improve this answer


























          • All modifications add a child theme to the functions.php, unfortunately in this case it receives the HTTP ERROR 500, and the page stops working.

            – dezajner
            Nov 21 '18 at 7:59













          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%2f53403845%2fdisabling-the-purchase-for-one-product-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









          0














          This code is just a bit outdated since Woocommerce 3, as $product->id need to be replaced with $product->get_id()… Try the following instead (where 534 is the related product ID where add to cart button will not appear):



          add_filter('woocommerce_is_purchasable', 'purchasable_product_customizations', 10, 2 );
          function purchasable_product_customizations( $is_purchasable, $product ) {
          if( in_array( $product->get_id() == 534 )
          $is_purchasable = false;

          return $is_purchasable;
          }


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





          If your product ID is a variable product



          For a variable product ID where you want to disable allvariations, you will replace this line:



          if( in_array( $product->get_id() == 534 )


          by this one:



          if( in_array( $product->get_parent_id() == 534 )



          If it still doesn't work is that something else is interacting and making trouble, like some customizations made by you, your theme or a plugin… Also it can be because you are not adding this code in the right place.







          share|improve this answer


























          • All modifications add a child theme to the functions.php, unfortunately in this case it receives the HTTP ERROR 500, and the page stops working.

            – dezajner
            Nov 21 '18 at 7:59


















          0














          This code is just a bit outdated since Woocommerce 3, as $product->id need to be replaced with $product->get_id()… Try the following instead (where 534 is the related product ID where add to cart button will not appear):



          add_filter('woocommerce_is_purchasable', 'purchasable_product_customizations', 10, 2 );
          function purchasable_product_customizations( $is_purchasable, $product ) {
          if( in_array( $product->get_id() == 534 )
          $is_purchasable = false;

          return $is_purchasable;
          }


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





          If your product ID is a variable product



          For a variable product ID where you want to disable allvariations, you will replace this line:



          if( in_array( $product->get_id() == 534 )


          by this one:



          if( in_array( $product->get_parent_id() == 534 )



          If it still doesn't work is that something else is interacting and making trouble, like some customizations made by you, your theme or a plugin… Also it can be because you are not adding this code in the right place.







          share|improve this answer


























          • All modifications add a child theme to the functions.php, unfortunately in this case it receives the HTTP ERROR 500, and the page stops working.

            – dezajner
            Nov 21 '18 at 7:59
















          0












          0








          0







          This code is just a bit outdated since Woocommerce 3, as $product->id need to be replaced with $product->get_id()… Try the following instead (where 534 is the related product ID where add to cart button will not appear):



          add_filter('woocommerce_is_purchasable', 'purchasable_product_customizations', 10, 2 );
          function purchasable_product_customizations( $is_purchasable, $product ) {
          if( in_array( $product->get_id() == 534 )
          $is_purchasable = false;

          return $is_purchasable;
          }


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





          If your product ID is a variable product



          For a variable product ID where you want to disable allvariations, you will replace this line:



          if( in_array( $product->get_id() == 534 )


          by this one:



          if( in_array( $product->get_parent_id() == 534 )



          If it still doesn't work is that something else is interacting and making trouble, like some customizations made by you, your theme or a plugin… Also it can be because you are not adding this code in the right place.







          share|improve this answer















          This code is just a bit outdated since Woocommerce 3, as $product->id need to be replaced with $product->get_id()… Try the following instead (where 534 is the related product ID where add to cart button will not appear):



          add_filter('woocommerce_is_purchasable', 'purchasable_product_customizations', 10, 2 );
          function purchasable_product_customizations( $is_purchasable, $product ) {
          if( in_array( $product->get_id() == 534 )
          $is_purchasable = false;

          return $is_purchasable;
          }


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





          If your product ID is a variable product



          For a variable product ID where you want to disable allvariations, you will replace this line:



          if( in_array( $product->get_id() == 534 )


          by this one:



          if( in_array( $product->get_parent_id() == 534 )



          If it still doesn't work is that something else is interacting and making trouble, like some customizations made by you, your theme or a plugin… Also it can be because you are not adding this code in the right place.








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 24 '18 at 3:26

























          answered Nov 21 '18 at 1:33









          LoicTheAztecLoicTheAztec

          90.6k1365104




          90.6k1365104













          • All modifications add a child theme to the functions.php, unfortunately in this case it receives the HTTP ERROR 500, and the page stops working.

            – dezajner
            Nov 21 '18 at 7:59





















          • All modifications add a child theme to the functions.php, unfortunately in this case it receives the HTTP ERROR 500, and the page stops working.

            – dezajner
            Nov 21 '18 at 7:59



















          All modifications add a child theme to the functions.php, unfortunately in this case it receives the HTTP ERROR 500, and the page stops working.

          – dezajner
          Nov 21 '18 at 7:59







          All modifications add a child theme to the functions.php, unfortunately in this case it receives the HTTP ERROR 500, and the page stops working.

          – dezajner
          Nov 21 '18 at 7:59






















          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%2f53403845%2fdisabling-the-purchase-for-one-product-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

          How to change which sound is reproduced for terminal bell?

          Can I use Tabulator js library in my java Spring + Thymeleaf project?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents