Remove add cart button in Woocommerce for a specific product category

醉酒当歌 提交于 2020-12-05 11:35:22

问题


I have an issue how to remove a cart from a category product. It works just fine if I apply it to a specific id or all in general, but I am unable to do it for a category. Below is my code I have done regarding it.

Also, I am struggling to apply this same pattern to Related Articles section, so any help would be appreciated.

Thank you.

//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');

}

回答1:


Nov 2020 Update

To make it work with a product category you can use the WordPress conditional function has_term() this way:

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 );
    }
}

Code goes in function.php file of your active child theme (or active theme). Or also in any plugin php file. Tested and works.




回答2:


You have try something like this:

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');

}

Category products in Woocommerce are simply terms. Wp_get_post_terms allow you to find any categories associated to post (product id).

Reference: https://codex.wordpress.org/Function_Reference/wp_get_post_terms




回答3:


You can try something like this

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' );

Reference : https://www.themelocation.com/how-to-remove-add-to-cart-button-from-a-certain-category-woocommerce/




回答4:


Use this code

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

Reference : https://www.damiencarbery.com/2020/03/remove-add-to-cart-button-conditionally/



来源:https://stackoverflow.com/questions/41660835/remove-add-cart-button-in-woocommerce-for-a-specific-product-category

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!