Last active
February 4, 2022 11:48
-
-
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/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| Plugin Name: Remove 'Add to cart' conditionally | |
| Plugin URI: https://www.damiencarbery.com/2020/03/remove-add-to-cart-conditionally/ | |
| Description: Conditionally remove the 'Add to cart' button in WooCommerce. | |
| Author: Damien Carbery | |
| Version: 0.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 ); | |
| add_filter( 'woocommerce_variation_is_purchasable', array( $this, 'variation_is_purchasable_conditionals' ), 10, 2 ); | |
| } | |
| public function is_purchasable_conditionals( $whether_purchasable, $product ) { | |
| // Return cached result. | |
| if ( array_key_exists( $product->get_id(), $this->purchasable ) ) { | |
| return $this->purchasable[ $product->get_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. | |
| if ( $product->get_price() > 20 ) { | |
| $result = false; | |
| } | |
| // Check if product in a certain categores. | |
| if ( has_term( array('hoodies', 'accessories' ), 'product_cat', $product->get_id() ) ) { | |
| $result = false; | |
| } | |
| $this->purchasable[ $product->get_id() ] = $result; | |
| } | |
| else { | |
| // Store that this item cannot be purchased. | |
| $this->purchasable[ $product->get_id() ] = false; | |
| } | |
| return $this->purchasable[ $product->get_id() ]; | |
| } | |
| public function variation_is_purchasable_conditionals( $whether_purchasable, $product ) { | |
| return $whether_purchasable; | |
| } | |
| } | |
| $IsPurchasableConditionalFiltering = new IsPurchasableConditionalFiltering; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment