Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide Add To Cart Button based on product custom field and product category in WooCommerce

Tags:

I would like to add a custom check box to the woocommerce product page back end that hides the Add To Cart button in the front end. I don't want to remove the ability to purchase the item entirely (would still like to be able to use a direct add to cart url to add the item to a cart and purchase) so I don't want to use add_filter('woocommerce_is_purchasable', 'my_woocommerce_is_purchasable', 10, 2);or similar.

What I currently have achieved is to :

Add the custom check box

// Display Checkbox
add_action('woocommerce_product_options_general_product_data', 'product_custom_fields_add');
function product_custom_fields_add(){
    global $post;

    echo '<div class="product_custom_field">';

    // Custom Product Checkbox Field
    woocommerce_wp_checkbox( array(
        'id'        => '_no_addcart_product',
        'desc'      => __('show or hide add to cart', 'woocommerce'),
        'label'     => __('Hide Add To Cart', 'woocommerce'),
        'desc_tip'  => 'true'
    ));

    echo '</div>';
}

// Save Checkbox
add_action('woocommerce_process_product_meta', 'product_custom_fields_save');
function product_custom_fields_save($post_id){
    // Custom Product Text Field
    $no_addcart_product = isset( $_POST['_no_addcart_product'] ) ? 'yes' : 'no';
        update_post_meta($post_id, '_no_addcart_product', esc_attr( $no_addcart_product ));
}

And also a Hide Add To Cart button depending on category snippet.

function remove_product_description_add_cart_button(){
    global $product;      
    $category = 'hide';        
    if ( has_term( $category, 'product_cat', $product->id ) )
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}

add_action('wp','remove_product_description_add_cart_button');

Both of the above work fine on their own. Where I'm failing is trying to combine them just to remove the add to cart button if the checkbox is active (I do not require to check the category as well). I put together the code below hoping it would work but it does not.

function remove_product_description_add_cart_button() {
    if ( is_product() && get_post_meta( $post->ID, '_no_addcart_product', true ) == 'yes' ) {
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}

add_action('wp','remove_product_description_add_cart_button');

Many thanks in advance to any help in the right direction.

like image 253
Acephalia Avatar asked Feb 23 '19 10:02

Acephalia


People also ask

How do I hide the shopping cart in WooCommerce?

To do this, please navigate to the Dashboard > Installed Plugins page. Furthermore, find the WooCommerce section and select the Deactivate option then the cart icon will be instantly deleted from your website.


1 Answers

You can combine both conditions with a relational OR argument this way:

add_action( 'woocommerce_single_product_summary', 'remove_product_add_to_cart_button', 4 );
function remove_product_add_to_cart_button(){
    global $product;

    $term_slug = 'hide'; // Product category term slug

    if ( has_term( $term_slug, 'product_cat', $product->get_id() ) || $product->get_meta('_no_addcart_product') === 'yes' )
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}

Code goes in function.php file of your active child theme (or active theme). It should work.

Note: Since Woocommerce 3 the WC_Product properties can't be accessed directly so you need to use the available methods with the WC_Product Object $product.

  • For the product ID you will use get_id() method like: $product->get_id()
  • For product custom meta data you can also use get_meta() method
like image 170
LoicTheAztec Avatar answered Oct 02 '22 05:10

LoicTheAztec