Activate a custom button once product is added to cart on Woocommerce single product page












1















In Woocommerce single product pages, I've added a custom button using the woocommerce_after_add_to_cart hook. The button will, when clicked/ pressed, re-directs the customer to the checkout.



But to avoid the customer clicking the button before having added the product to the cart, I added this: if ( WC()->cart->get_cart_contents_count() != 0 ) { to the function.



My question is this: how can I have this button always available but inactive until a product is added to cart? How do I make it "grayed out" (not clickable) until there is a product in the cart?



Here is my complete code:



add_action('woocommerce_after_add_to_cart_button, 'instant_checkout');
function instant_checkout() {
$checkout_url = WC()->cart->get_checkout_url();
if ( WC()->cart->get_cart_contents_count() != 0 ) {
echo '<a href="'.$checkout_url.'" class="single_add_to_cart_button button alt">Instant Checkout</a>'; } }


Thanks for any help on this.










share|improve this question





























    1















    In Woocommerce single product pages, I've added a custom button using the woocommerce_after_add_to_cart hook. The button will, when clicked/ pressed, re-directs the customer to the checkout.



    But to avoid the customer clicking the button before having added the product to the cart, I added this: if ( WC()->cart->get_cart_contents_count() != 0 ) { to the function.



    My question is this: how can I have this button always available but inactive until a product is added to cart? How do I make it "grayed out" (not clickable) until there is a product in the cart?



    Here is my complete code:



    add_action('woocommerce_after_add_to_cart_button, 'instant_checkout');
    function instant_checkout() {
    $checkout_url = WC()->cart->get_checkout_url();
    if ( WC()->cart->get_cart_contents_count() != 0 ) {
    echo '<a href="'.$checkout_url.'" class="single_add_to_cart_button button alt">Instant Checkout</a>'; } }


    Thanks for any help on this.










    share|improve this question



























      1












      1








      1








      In Woocommerce single product pages, I've added a custom button using the woocommerce_after_add_to_cart hook. The button will, when clicked/ pressed, re-directs the customer to the checkout.



      But to avoid the customer clicking the button before having added the product to the cart, I added this: if ( WC()->cart->get_cart_contents_count() != 0 ) { to the function.



      My question is this: how can I have this button always available but inactive until a product is added to cart? How do I make it "grayed out" (not clickable) until there is a product in the cart?



      Here is my complete code:



      add_action('woocommerce_after_add_to_cart_button, 'instant_checkout');
      function instant_checkout() {
      $checkout_url = WC()->cart->get_checkout_url();
      if ( WC()->cart->get_cart_contents_count() != 0 ) {
      echo '<a href="'.$checkout_url.'" class="single_add_to_cart_button button alt">Instant Checkout</a>'; } }


      Thanks for any help on this.










      share|improve this question
















      In Woocommerce single product pages, I've added a custom button using the woocommerce_after_add_to_cart hook. The button will, when clicked/ pressed, re-directs the customer to the checkout.



      But to avoid the customer clicking the button before having added the product to the cart, I added this: if ( WC()->cart->get_cart_contents_count() != 0 ) { to the function.



      My question is this: how can I have this button always available but inactive until a product is added to cart? How do I make it "grayed out" (not clickable) until there is a product in the cart?



      Here is my complete code:



      add_action('woocommerce_after_add_to_cart_button, 'instant_checkout');
      function instant_checkout() {
      $checkout_url = WC()->cart->get_checkout_url();
      if ( WC()->cart->get_cart_contents_count() != 0 ) {
      echo '<a href="'.$checkout_url.'" class="single_add_to_cart_button button alt">Instant Checkout</a>'; } }


      Thanks for any help on this.







      php wordpress woocommerce product cart






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 16:15









      LoicTheAztec

      85.5k136095




      85.5k136095










      asked Nov 13 '18 at 14:26







      user10551357































          1 Answer
          1






          active

          oldest

          votes


















          0














          The following will display a custom disabled button after add to cart button… When the product will be added to cart the product will be enabled and linked to checkout page:



          add_action('woocommerce_after_add_to_cart_button', 'get_instant_checkout_buttom_html');
          function get_instant_checkout_buttom_html() {
          global $product;

          $href = ''; // Initializing
          $class = ' disabled'; // Initializing

          // Continue only if cart is not empty
          if ( ! WC()->cart->is_empty() ) {
          // Loop through cart items
          foreach( WC()->cart->get_cart() as $item ) {
          // When product is in cart
          if( $item['product_id'] == $product->get_id() ){
          $href = ' href="'.wc_get_checkout_url().'"'; // Add the link to the button
          $class = ''; // Remove "disabled" class
          break; // Stop the loop
          }
          }
          }
          // Button output
          echo '<a'.$href.' class="single_add_to_cart_button button alt'.$class.'">'.__("Instant Checkout").'</a>';
          }


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






          share|improve this answer


























          • thanks for the answer, but it does exactly what my code does? It does not display the button until there is a product in the cart. I need the button visible, just not clickable, until a product is added to cart.

            – user10551357
            Nov 13 '18 at 15:04











          • @WooDevil Updated… try it

            – LoicTheAztec
            Nov 13 '18 at 15:18











          • thank you for your help once again, works like a charm :)

            – user10551357
            Nov 13 '18 at 15:19











          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%2f53283197%2factivate-a-custom-button-once-product-is-added-to-cart-on-woocommerce-single-pro%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














          The following will display a custom disabled button after add to cart button… When the product will be added to cart the product will be enabled and linked to checkout page:



          add_action('woocommerce_after_add_to_cart_button', 'get_instant_checkout_buttom_html');
          function get_instant_checkout_buttom_html() {
          global $product;

          $href = ''; // Initializing
          $class = ' disabled'; // Initializing

          // Continue only if cart is not empty
          if ( ! WC()->cart->is_empty() ) {
          // Loop through cart items
          foreach( WC()->cart->get_cart() as $item ) {
          // When product is in cart
          if( $item['product_id'] == $product->get_id() ){
          $href = ' href="'.wc_get_checkout_url().'"'; // Add the link to the button
          $class = ''; // Remove "disabled" class
          break; // Stop the loop
          }
          }
          }
          // Button output
          echo '<a'.$href.' class="single_add_to_cart_button button alt'.$class.'">'.__("Instant Checkout").'</a>';
          }


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






          share|improve this answer


























          • thanks for the answer, but it does exactly what my code does? It does not display the button until there is a product in the cart. I need the button visible, just not clickable, until a product is added to cart.

            – user10551357
            Nov 13 '18 at 15:04











          • @WooDevil Updated… try it

            – LoicTheAztec
            Nov 13 '18 at 15:18











          • thank you for your help once again, works like a charm :)

            – user10551357
            Nov 13 '18 at 15:19
















          0














          The following will display a custom disabled button after add to cart button… When the product will be added to cart the product will be enabled and linked to checkout page:



          add_action('woocommerce_after_add_to_cart_button', 'get_instant_checkout_buttom_html');
          function get_instant_checkout_buttom_html() {
          global $product;

          $href = ''; // Initializing
          $class = ' disabled'; // Initializing

          // Continue only if cart is not empty
          if ( ! WC()->cart->is_empty() ) {
          // Loop through cart items
          foreach( WC()->cart->get_cart() as $item ) {
          // When product is in cart
          if( $item['product_id'] == $product->get_id() ){
          $href = ' href="'.wc_get_checkout_url().'"'; // Add the link to the button
          $class = ''; // Remove "disabled" class
          break; // Stop the loop
          }
          }
          }
          // Button output
          echo '<a'.$href.' class="single_add_to_cart_button button alt'.$class.'">'.__("Instant Checkout").'</a>';
          }


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






          share|improve this answer


























          • thanks for the answer, but it does exactly what my code does? It does not display the button until there is a product in the cart. I need the button visible, just not clickable, until a product is added to cart.

            – user10551357
            Nov 13 '18 at 15:04











          • @WooDevil Updated… try it

            – LoicTheAztec
            Nov 13 '18 at 15:18











          • thank you for your help once again, works like a charm :)

            – user10551357
            Nov 13 '18 at 15:19














          0












          0








          0







          The following will display a custom disabled button after add to cart button… When the product will be added to cart the product will be enabled and linked to checkout page:



          add_action('woocommerce_after_add_to_cart_button', 'get_instant_checkout_buttom_html');
          function get_instant_checkout_buttom_html() {
          global $product;

          $href = ''; // Initializing
          $class = ' disabled'; // Initializing

          // Continue only if cart is not empty
          if ( ! WC()->cart->is_empty() ) {
          // Loop through cart items
          foreach( WC()->cart->get_cart() as $item ) {
          // When product is in cart
          if( $item['product_id'] == $product->get_id() ){
          $href = ' href="'.wc_get_checkout_url().'"'; // Add the link to the button
          $class = ''; // Remove "disabled" class
          break; // Stop the loop
          }
          }
          }
          // Button output
          echo '<a'.$href.' class="single_add_to_cart_button button alt'.$class.'">'.__("Instant Checkout").'</a>';
          }


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






          share|improve this answer















          The following will display a custom disabled button after add to cart button… When the product will be added to cart the product will be enabled and linked to checkout page:



          add_action('woocommerce_after_add_to_cart_button', 'get_instant_checkout_buttom_html');
          function get_instant_checkout_buttom_html() {
          global $product;

          $href = ''; // Initializing
          $class = ' disabled'; // Initializing

          // Continue only if cart is not empty
          if ( ! WC()->cart->is_empty() ) {
          // Loop through cart items
          foreach( WC()->cart->get_cart() as $item ) {
          // When product is in cart
          if( $item['product_id'] == $product->get_id() ){
          $href = ' href="'.wc_get_checkout_url().'"'; // Add the link to the button
          $class = ''; // Remove "disabled" class
          break; // Stop the loop
          }
          }
          }
          // Button output
          echo '<a'.$href.' class="single_add_to_cart_button button alt'.$class.'">'.__("Instant Checkout").'</a>';
          }


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







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 15:19

























          answered Nov 13 '18 at 14:41









          LoicTheAztecLoicTheAztec

          85.5k136095




          85.5k136095













          • thanks for the answer, but it does exactly what my code does? It does not display the button until there is a product in the cart. I need the button visible, just not clickable, until a product is added to cart.

            – user10551357
            Nov 13 '18 at 15:04











          • @WooDevil Updated… try it

            – LoicTheAztec
            Nov 13 '18 at 15:18











          • thank you for your help once again, works like a charm :)

            – user10551357
            Nov 13 '18 at 15:19



















          • thanks for the answer, but it does exactly what my code does? It does not display the button until there is a product in the cart. I need the button visible, just not clickable, until a product is added to cart.

            – user10551357
            Nov 13 '18 at 15:04











          • @WooDevil Updated… try it

            – LoicTheAztec
            Nov 13 '18 at 15:18











          • thank you for your help once again, works like a charm :)

            – user10551357
            Nov 13 '18 at 15:19

















          thanks for the answer, but it does exactly what my code does? It does not display the button until there is a product in the cart. I need the button visible, just not clickable, until a product is added to cart.

          – user10551357
          Nov 13 '18 at 15:04





          thanks for the answer, but it does exactly what my code does? It does not display the button until there is a product in the cart. I need the button visible, just not clickable, until a product is added to cart.

          – user10551357
          Nov 13 '18 at 15:04













          @WooDevil Updated… try it

          – LoicTheAztec
          Nov 13 '18 at 15:18





          @WooDevil Updated… try it

          – LoicTheAztec
          Nov 13 '18 at 15:18













          thank you for your help once again, works like a charm :)

          – user10551357
          Nov 13 '18 at 15:19





          thank you for your help once again, works like a charm :)

          – user10551357
          Nov 13 '18 at 15:19


















          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%2f53283197%2factivate-a-custom-button-once-product-is-added-to-cart-on-woocommerce-single-pro%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







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini