Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active February 4, 2022 11:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damiencarbery/d430d6beeab9dde964236f35ba67b5ba to your computer and use it in GitHub Desktop.
Save damiencarbery/d430d6beeab9dde964236f35ba67b5ba to your computer and use it in GitHub Desktop.
Remove 'Add to cart' conditionally - Conditionally remove the 'Add to cart' button in WooCommerce. https://www.damiencarbery.com/2020/03/remove-add-to-cart-conditionally/
<?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.1.0
*/
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.
// 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;
}*/
/* // Check if product is in a list of IDs:
$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();
}
// 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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment