skip to Main Content

as the title states, I’m trying to remove the buy button for virtual products. I am using the Astra theme in WordPress alongside Woocommerce, and Elementor.

Now my issue is that with my current implementation, it does remove the button from the individual product page. However it still displays the button on the categories page, which is a pain. I have tried setting the price to nothing, and while that does work. It doesn’t help as I still want the products to have their prices listed.

I have added the following code to the functions.php section of my duplicated theme file;

function buy_filter()
{
    if ( ! is_product() ) return;
    
    $product = get_product();
    
    if ($product->is_virtual('yes'))
    {
        //$product->is_purchasable('false')
        
        //remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );
        //remove_action('woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30); 
        
        remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30);
        remove_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30);
        remove_action( 'woocommerce_grouped_add_to_cart', 'woocommerce_grouped_add_to_cart', 30);
        remove_action( 'woocommerce_variable_add_to_cart', 'woocommerce_variable_add_to_cart', 30);
        remove_action( 'woocommerce_external_add_to_cart', 'woocommerce_external_add_to_cart', 30);
    }
}

add_action ('wp', 'buy_filter');

Any help would be greatly appreciated, and I’m happy to give any more details I can to aid in the matter.

Thank you kindly!

2

Answers


  1. You need to get the specific product by it’s ID, I have modified a few part of your code and removed few of the hooks you used. Please add those if required.

    function woocommerce_remove_cart()
    {
        if ( ! is_product() ) return;
        $product_id=get_the_ID();
        $product = wc_get_product($product_id);
        if ($product->is_virtual('yes'))
        {
    
            remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
        }
    
    }//end of function
    add_action('wp_head','woocommerce_remove_cart');
    

    Hope it works.

    Login or Signup to reply.
  2. add_filter( 'woocommerce_is_purchasable', 'woocommerce_isproduct_purchasable', 10, 2 );
    
    function woocommerce_isproduct_purchasable( $purchasable, $product ) {
        return ($product->is_virtual( 'yes' ) ) ? false : $purchasable;
    }
    

    This is the hook you are looking for

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search