在 Woocommerce 中删除特定产品类别的添加购物车按钮
Remove add cart button in Woocommerce for a specific product category
我遇到了如何从类别产品中删除购物车的问题。如果我将它应用于特定的 id 或一般的所有 id,它就可以正常工作,但我无法对一个类别执行此操作。下面是我已经完成的代码。
此外,我正在努力将相同的模式应用到相关文章部分,因此我们将不胜感激。
谢谢。
//function for deleting ....
function remove_product_description_add_cart_button(){
global $product;
//Remove Add to Cart button from product description of product with id 1234
if ($product->id == 188){
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
add_action('wp','remove_product_description_add_cart_button');
}
你试过这样的事情:
function remove_product_description_add_cart_button(){
global $product;
$termsOfProduct = wp_get_post_terms( $product->id, 'product_cat' );
if (in_array("CatToFind", $termsOfProduct)) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
add_action('wp','remove_product_description_add_cart_button');
}
Woocommerce 中的类别产品只是术语。 Wp_get_post_terms 允许您查找与 post(产品 ID)关联的任何类别。
参考:https://codex.wordpress.org/Function_Reference/wp_get_post_terms
2020 年 11 月更新
要使其与产品类别一起使用,您可以这样使用 WordPress 条件函数 has_term()
:
add_action('woocommerce_single_product_summary', 'remove_product_description_add_cart_button', 1 );
function remove_product_description_add_cart_button() { // function for deleting ...
// Set HERE your category ID, slug or name (or an array)
$categories = array('your-category-1');
//Remove Add to Cart button from product description of product with id 1234
if ( has_term( $categories, 'product_cat', get_the_id() ) ) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。或者也可以在任何插件 php 文件中。 已测试并有效。
你可以试试这个
function western_custom_buy_buttons(){
$product = get_product();
if ( has_term( 'category1', 'product_cat') ){
// removing the purchase buttons
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
remove_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 );
remove_action( 'woocommerce_grouped_add_to_cart', 'woocommerce_grouped_add_to_cart', 30 );
remove_action( 'woocommerce_variable_add_to_cart', 'woocommerce_variable_add_to_cart', 30 );
remove_action( 'woocommerce_external_add_to_cart', 'woocommerce_external_add_to_cart', 30 );
}
}
add_action( 'wp', 'western_custom_buy_buttons' );
参考:https://www.themelocation.com/how-to-remove-add-to-cart-button-from-a-certain-category-woocommerce/
使用此代码
/*
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.2
*/
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 );
// 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 ) {
// 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() > 2 ) {
$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;
}
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;
参考:https://www.damiencarbery.com/2020/03/remove-add-to-cart-button-conditionally/
我遇到了如何从类别产品中删除购物车的问题。如果我将它应用于特定的 id 或一般的所有 id,它就可以正常工作,但我无法对一个类别执行此操作。下面是我已经完成的代码。
此外,我正在努力将相同的模式应用到相关文章部分,因此我们将不胜感激。
谢谢。
//function for deleting ....
function remove_product_description_add_cart_button(){
global $product;
//Remove Add to Cart button from product description of product with id 1234
if ($product->id == 188){
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
add_action('wp','remove_product_description_add_cart_button');
}
你试过这样的事情:
function remove_product_description_add_cart_button(){
global $product;
$termsOfProduct = wp_get_post_terms( $product->id, 'product_cat' );
if (in_array("CatToFind", $termsOfProduct)) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
add_action('wp','remove_product_description_add_cart_button');
}
Woocommerce 中的类别产品只是术语。 Wp_get_post_terms 允许您查找与 post(产品 ID)关联的任何类别。
参考:https://codex.wordpress.org/Function_Reference/wp_get_post_terms
2020 年 11 月更新
要使其与产品类别一起使用,您可以这样使用 WordPress 条件函数 has_term()
:
add_action('woocommerce_single_product_summary', 'remove_product_description_add_cart_button', 1 );
function remove_product_description_add_cart_button() { // function for deleting ...
// Set HERE your category ID, slug or name (or an array)
$categories = array('your-category-1');
//Remove Add to Cart button from product description of product with id 1234
if ( has_term( $categories, 'product_cat', get_the_id() ) ) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。或者也可以在任何插件 php 文件中。 已测试并有效。
你可以试试这个
function western_custom_buy_buttons(){
$product = get_product();
if ( has_term( 'category1', 'product_cat') ){
// removing the purchase buttons
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
remove_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 );
remove_action( 'woocommerce_grouped_add_to_cart', 'woocommerce_grouped_add_to_cart', 30 );
remove_action( 'woocommerce_variable_add_to_cart', 'woocommerce_variable_add_to_cart', 30 );
remove_action( 'woocommerce_external_add_to_cart', 'woocommerce_external_add_to_cart', 30 );
}
}
add_action( 'wp', 'western_custom_buy_buttons' );
参考:https://www.themelocation.com/how-to-remove-add-to-cart-button-from-a-certain-category-woocommerce/
使用此代码
/*
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.2
*/
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 );
// 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 ) {
// 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() > 2 ) {
$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;
}
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;
参考:https://www.damiencarbery.com/2020/03/remove-add-to-cart-button-conditionally/