【问题标题】:Remove add cart button in Woocommerce for a specific product category删除 Woocommerce 中特定产品类别的添加购物车按钮
【发布时间】:2017-05-30 08:57:46
【问题描述】:

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

}

【问题讨论】:

    标签: php wordpress woocommerce product custom-taxonomy


    【解决方案1】:

    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 文件中。经过测试并且可以工作。

    【讨论】:

    • LoicTheAztec 解决方案有效,但它也隐藏了可变产品变体。 sn-p 删除“添加到购物车”按钮,所有变量、颜色、尺寸。解决方案需要适当改进。
    • @ArtiosMedia 我在这里的回答仅与使用问题作者的初始代码使用has_term() 条件函数来定位特定产品类别有关。
    • 此代码返回给我Uncaught Error: Call to a member function get_meta() on string。实际上做一个简单的echo $product; 我得到一个字符串(产品的蛞蝓)。也许它只发生在我的特定主题(波尔图)
    • @MarcoPanichi 我已经更新了代码,现在可以完美运行了。
    • @evavienna 只需在具有相同优先级的 remove_action 之后使用 add_action 并在外部使用自定义函数来显示一条消息……
    【解决方案2】:

    你可以试试这样的

    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/

    【讨论】:

      【解决方案3】:

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

      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

      【讨论】:

        【解决方案4】:

        使用此代码

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-12-13
          • 2018-02-10
          • 1970-01-01
          • 2019-08-04
          • 2014-05-26
          • 1970-01-01
          • 1970-01-01
          • 2012-08-21
          相关资源
          最近更新 更多