繁体   English   中英

删除 Woocommerce 中特定产品类别的添加购物车按钮

[英]Remove add cart button in Woocommerce for a specific product category

我对如何从类别产品中删除购物车有疑问。 如果我将它应用于特定的 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');

}

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/

你已经尝试过这样的事情:

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 允许您查找与帖子(产品 ID)相关的任何类别。

参考: https : //codex.wordpress.org/Function_Reference/wp_get_post_terms

使用此代码

/*
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/

您可以在第 3 行添加您自己的类别名称

add_filter('woocommerce_is_purchasable', 'set_catalog_mode_on_for_category', 10, 2 );
 function set_catalog_mode_on_for_category( $is_purchasable, $product ) {
 if( has_term( 'Category Name', 'product_cat', $product->get_id() ) ) { //Change Category name here
    return false;  }
  return $is_purchasable;
}

资料来源: https://webtalkhub.com/how-to-remove-add-to-cart-button-in-woocommerce/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM