skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    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.

    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).


  2. 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;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search