John Davidson

php - Disallow purchases from virtual products in WooCommerce

0 comments
Message:


I'm trying to remove the add to cart button from the categories page for products that are not purchasable.


This is my current code, which I have placed in the functions.php file of my child theme;


function buy_filter()
{
if ( ! is_product() ) return;
$product_id=get_the_ID();
$product = wc_get_product($product_id);
if ($product->is_virtual('yes'))

if ($product->is_virtual('yes'))
{
add_filter( 'woocommerce_is_purchasable', '__return_false');
}
}

add_action ('wp', 'buy_filter');

I've managed to remove the button from the individual product pages, however I still can't get it to be removed from the categories page. Now when I inspect the button shows the following code:


<a href="?add-to-cart=16972" data-quantity="1" class="button product_type_simple add_to_cart_button
ajax_add_to_cart" data-product_id="16972" data-product_sku="604544617405" aria-label=
"Add “Federal - 9MM Syntech, 115gr” to your cart" rel="nofollow">Add to cart</a>

Does this button need to be disabled in another way?


I really need this button to be removed as its a product that we only want to sell in our store, but we want people to know that we do stock that product as well.


Below is an image of what it currently looks like, and what I want to go. Ideally, I would like the button gone completely, but I will definitely settle for a replacement to the read more button if that is easier.




My Answer:
>is_virtual()) {_ remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10);_ }_}_add_action('wp', 'buy_filter');_However, this code only removes the add to cart button for virtual products on the single product page, not on the categories page. To remove the add to cart button for virtual products on the categories page, you can modify the code as follows:_function buy_filter()_{_ if ( ! is_product() && ! is_shop() ) return;_ global $product;_ if ($product->is_virtual()) {_ remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10);_ }_}_add_action('wp', 'buy_filter');_This code checks if the current page is a product page or the shop page (categories page) and removes the add to cart button for virtual products on both pages.

Rate this post

4 of 5 based on 3497 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us