Home > Back-end >  Hide enquiry button when product is on stock
Hide enquiry button when product is on stock

Time:01-11

How can I hide a specific button, based on the stock status of my product?

The plugin is creating this class:


    function wdm_pefree_init() {
    // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
    if ( ! class_exists( 'Product_Enquiry_For_Woocommerce', false ) ) {
        include_once WDM_PE_PLUGIN_PATH . '/includes/class-product-enquiry-for-woocommerce.php';
    }
    Product_Enquiry_For_Woocommerce::instance();
    }

I only want to display this button the single product page of every product that is in backorder, but I can't get my code to work.

I'm not that great with PHP, so I'm trying to adapt some other code I have on my functions.php file, but without any luck.

Any help would be great, thanks!

I've tried this code:

    add_filter('woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);
    function wcs_custom_get_availability($availability, $_product) {
    // Remove Enquiry Button
      if (!$_product->is_in_stock()) {
          remove_action('Product_Enquiry_For_Woocommerce');
      }
      return $availability;
    }

I also see that the css class for the button is .pe-show-enq-modal, but I can't do a conditional "visibility: hidden" that only works for backorder products.

CodePudding user response:

What you are looking for is this:

add_action( 'woocommerce_single_product_summary', 'remove_enquiry_button_if_out_of_stock', 30 );
function remove_enquiry_button_if_out_of_stock() {
    global $product;
    if ( ! $product->is_in_stock() ) {
        remove_action( 'woocommerce_single_product_summary', array( Product_Enquiry_For_Woocommerce::instance(), 'enquiry_button' ), 25 );
    }
}

Or Via CSS :

.product.out-of-stock .pe-show-enq-modal {
    display: none;
}

CodePudding user response:

In the function wcs_custom_get_availability() you're trying to use remove_action('Product_Enquiry_For_Woocommerce');but that won't work as the remove_action() function expects two arguments: the name of the action hook, and the callback function that should be removed.

You should first check if the product is in stock or not and based on that conditionally add class to the button and with css you can hide or show it.

You can use this code below :

    add_filter('woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);
function wcs_custom_get_availability($availability, $_product) {
    // check if product is out of stock
    if (!$_product->is_in_stock()) {
        $availability['class'] = 'out-of-stock';
    }
    return $availability;
}

And in your css you can add

.pe-show-enq-modal.out-of-stock {
    display: none;
}

This will add a class of 'out-of-stock' to the

element that contains the availability information if the product is out of stock, which you can then use to hide the enquiry button with CSS.

Another way is to use jquery to check the class and hide the button if that class is present:

 jQuery(document).ready(function($) {
  if ($('.pe-show-enq-modal').hasClass('out-of-stock')) {
    $('.pe-show-enq-modal').hide();
  }
});

You will also have to make sure that the css and js are only included on product page.

CodePudding user response:

The only way I got this (kinda) working was by using this JS:

function remove_enquiry() {
?>
      <script type="text/javascript">
        (function($){

$(document).ready(function(){
   if($(".in-stock").length)
            $(".pe-show-enq-modal").hide();
});
})(jQuery);
      </script>
  <?php
}
add_action('wp_head', 'remove_enquiry');

You can see it here: https://lavaredamusicshop.pt/produto/teclados-e-pianos/teclados/teclado-casio-ct-x700/

But if you notice, it has a delay executing the JS, because when the page loads, the button still shows up for a brief moment (otherwise it seems to be working ok).

  • Related