Fuente: https://www.damiencarbery.com/2020/03/remove-add-to-cart-button-conditionally/
Si a veces has querido bloquear la compra por determinadas condiciones en woocommerce, este plugin es lo que necesitas.
<?php /* Plugin Name: Remove 'Add to cart' conditionally Plugin URI: https://www.damiencarbery.com/2020/03/remove-add-to-cart-button-conditionally/ Description: Conditionally remove the 'Add to cart' button in WooCommerce. Author: Damien Carbery Version: 0.4 WC tested up to: 5.9.1 */ class IsPurchasableConditionalFiltering { // A reference to an instance of this class. private static $instance; // Store whether 'Add to cart' button should be displayed. private $purchasable; // Returns an instance of this class. public static function get_instance() { if ( null == self::$instance ) { self::$instance = new IsPurchasableConditionalFiltering; } return self::$instance; } // Initialize the plugin variables. public function __construct() { $this->purchasable = array(); $this->init(); } // Set up WordPress specfic actions. public function init() { add_filter( 'woocommerce_is_purchasable', array( $this, 'is_purchasable_conditionals' ), 10, 2 ); // Remove variations dropdown and 'Add to cart' button for variable products. add_action( 'woocommerce_before_single_product_summary', array( $this, 'before_single_product_summary' ) ); } public function is_purchasable_conditionals( $whether_purchasable, $product ) { // Store the product ID as $product will be overwritten if $product is a variation. $product_id = $product->get_id(); // Return cached result. if ( array_key_exists( $product_id, $this->purchasable ) ) { return $this->purchasable[ $product_id ]; } // Only do our conditional checks if WooCommerce deems the item to be purchasable. if ( $whether_purchasable ) { $result = true; // Default to allowing purchase. // Aquí puedes bloquear por condiciones de precio, tienes que decomentar el bloque de código // Check our specific conditions - some examples. /* // Product over a certain price - this must be checked at variation // level so do this before the $product->get_type() check. if ( $product->get_price() > 20 ) { $result = false; }*/ /* // Aquí puedes poner las ids de productos a bloquear, tienes que decomentar el bloque de código $ids_not_purchasable = array ( 111, 140, 437, 1733, ); if ( in_array( $product_id, $ids_not_purchasable ) ) { $result = false; }*/ // If this is a variation then get the parent product so that categories can be checked. if ( 'variation' == $product->get_type() ) { $product = wc_get_product( $product->get_parent_id() ); $product_id = $product->get_id(); } // Aquí puedes bloquear por categoría, tienes que decomentar el bloque de código // Check if product in a certain categores. if ( has_term( array( 'hoodies', 'accessories', 'tshirts' ), 'product_cat', $product_id ) ) { $result = false; } $this->purchasable[ $product_id ] = $result; } else { // Store that this item cannot be purchased. $this->purchasable[ $product_id ] = false; } return $this->purchasable[ $product_id ]; } public function before_single_product_summary() { $product_id = get_the_ID(); if ( array_key_exists( $product_id, $this->purchasable ) && !$this->purchasable[ $product_id ] ) { // Remove the variation dropdowns and 'Add to cart' button for variable products. remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 ); remove_action( 'woocommerce_variable_add_to_cart', 'woocommerce_variable_add_to_cart', 30 ); } } } $IsPurchasableConditionalFiltering = new IsPurchasableConditionalFiltering;