Last active
February 4, 2022 11:48
-
-
Save damiencarbery/d430d6beeab9dde964236f35ba67b5ba to your computer and use it in GitHub Desktop.
Revisions
-
damiencarbery revised this gist
Feb 4, 2022 . 1 changed file with 9 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -4,7 +4,7 @@ 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 */ @@ -62,12 +62,19 @@ public function is_purchasable_conditionals( $whether_purchasable, $product ) { $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; } -
damiencarbery revised this gist
Feb 4, 2022 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ <?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.3 -
damiencarbery revised this gist
Apr 3, 2021 . 1 changed file with 59 additions and 60 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -10,86 +10,85 @@ 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; }*/ // 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() ); } // Check if product in a certain categores. if ( has_term( array( 'hoodies', 'accessories', 'tshirts' ), 'product_cat', $product->get_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; -
damiencarbery revised this gist
Apr 3, 2021 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -45,6 +45,7 @@ public function init() { 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(); error_log( sprintf( "Product: %s, ID: %d", $product->get_name(), $product_id ) ); // Return cached result. if ( array_key_exists( $product_id, $this->purchasable ) ) { @@ -66,13 +67,11 @@ public function is_purchasable_conditionals( $whether_purchasable, $product ) { if ( 'variation' == $product->get_type() ) { $product = wc_get_product( $product->get_parent_id() ); } // Check if product in a certain categores. if ( has_term( array( 'hoodies', 'accessories', 'tshirts' ), 'product_cat', $product->get_id() ) ) { $result = false; } $this->purchasable[ $product_id ] = $result; } else { @@ -87,8 +86,9 @@ public function is_purchasable_conditionals( $whether_purchasable, $product ) { 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 ); } } } -
damiencarbery revised this gist
Apr 2, 2021 . 1 changed file with 24 additions and 20 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -4,7 +4,8 @@ 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.3 WC tested up to: 5.1.0 */ @@ -35,57 +36,60 @@ public function __construct() { // 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; }*/ // 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() ); } // Check if product in a certain categores. if ( has_term( array( 'hoodies', 'accessories', 'tshirts' ), 'product_cat', $product->get_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_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; -
damiencarbery revised this gist
May 9, 2020 . 1 changed file with 17 additions and 5 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -4,7 +4,7 @@ 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.2 */ @@ -36,6 +36,9 @@ public function __construct() { 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 ); // Remove variations dropdown and 'Add to cart' button for variable products. add_action( 'woocommerce_before_single_product_summary', array( $this, 'before_single_product_summary' ) ); } @@ -50,13 +53,13 @@ public function is_purchasable_conditionals( $whether_purchasable, $product ) { $result = true; // Default to allowing purchase. // Check our specific conditions - some examples. /* // Product over a certain price. if ( $product->get_price() > 2 ) { $result = false; }*/ // Check if product in a certain categores. if ( has_term( array( 'hoodies', 'accessories' ), 'product_cat', $product->get_id() ) ) { $result = false; } @@ -75,5 +78,14 @@ public function is_purchasable_conditionals( $whether_purchasable, $product ) { public function variation_is_purchasable_conditionals( $whether_purchasable, $product ) { return $whether_purchasable; } public function before_single_product_summary() { $product_id = get_the_ID(); if ( array_key_exists( $product_id, $this->purchasable ) && !$this->purchasable[ $product_id ] ) { 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; -
damiencarbery renamed this gist
Mar 22, 2020 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,8 +1,8 @@ <?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 */ -
damiencarbery created this gist
Mar 22, 2020 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,79 @@ <?php /* Plugin Name: Hide 'Add to cart' conditionally Plugin URI: https://www.damiencarbery.com/2020/03/hide-add-to-cart-conditionally/ Description: Conditionally hide 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;