John Davidson

php - Woocommerce hide add to cart button EXCEPT for variable products

0 comments
Message:


Im trying to hide the add to cart button in woocommerce for all products except a varibale product i have. I have tried the following which leaves the variable select options (which is what i want) but it hides the add to cart button (which i don't want).


add_filter( 'woocommerce_is_purchasable', '__return_false' ); 

add_action( 'woocommerce_single_product_summary', 'hide_add_to_cart_button_variable_product', 1, 0 );
function hide_add_to_cart_button_variable_product() {

remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20);
}

Is there a way to do this?


All my products are simple products except for this single variable products, so maybe there is a function to hide the cart button for all simples except variables?



My Answer:
You can achieve this by checking if the product is a variable product before hiding the add to cart button. Here's an updated code snippet that should work for you:

php
add_action( 'woocommerce_single_product_summary', 'hide_add_to_cart_button_except_variable_product', 1 );

function hide_add_to_cart_button_except_variable_product() {
global $product;

// Check if the product is a variable product
if ( $product->is_type( 'variable' ) ) {
return;
}

// If not a variable product, remove the add to cart button
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}


This code will only hide the add to cart button for simple products and not for variable products.

Rate this post

3 of 5 based on 4560 votes

Comments




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