【问题标题】:Hide quantity fields in cart for specific products in Woocommerce 3在 Woocommerce 3 中隐藏特定产品的购物车中的数量字段
【发布时间】:2018-06-29 13:23:41
【问题描述】:

我想在购物车页面中隐藏一个特定产品类别的数量字段。

“单独出售”对我没有帮助,因为我有一个必须禁用才能工作的插件。

有可能吗?任何曲目将不胜感激。

【问题讨论】:

    标签: php wordpress woocommerce cart product-quantity


    【解决方案1】:

    以下代码将隐藏购物车页面上的产品数量字段:

    1) 针对特定产品类别(您将在此代码中定义)

    add_filter( 'woocommerce_quantity_input_args', 'hide_quantity_input_field', 20, 2 );
    function hide_quantity_input_field( $args, $product ) {
        // Here set your product categories in the array (can be either an ID, a slug, a name or an array)
        $categories = array('t-shirts','shoes');
    
        // Handling product variation
        $the_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
    
        // Only on cart page for a specific product category
        if( is_cart() && has_term( $categories, 'product_cat', $the_id ) ){
            $input_value = $args['input_value'];
            $args['min_value'] = $args['max_value'] = $input_value;
        }
        return $args;
    }
    

    代码进入活动子主题(或活动主题)的 function.php 文件中。测试和工作。
    它还将处理添加到购物车的产品变体。

    2) 特定产品 ID (您将在此代码中定义)

    add_filter( 'woocommerce_quantity_input_args', 'hide_quantity_input_field', 20, 2 );
    function hide_quantity_input_field( $args, $product ) {
        // Here set your product IDs in the array
        $product_ids = array(37,40,70);
    
        // Handling product variation
        $the_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
    
        // Only on cart page for a specific product category
        if( is_cart() && in_array( $the_id, $product_ids ) ){
            $input_value = $args['input_value'];
            $args['min_value'] = $args['max_value'] = $input_value;
        }
        return $args;
    }
    

    代码进入活动子主题(或活动主题)的 function.php 文件中。测试和工作。
    它还将处理添加到购物车的产品变体。

    【讨论】:

    • 如果您有多个产品类别?目前它只有两个类别。以 cat1 cat2 为例。谢谢
    • @GeorgeCh 更新了我的代码以处理多个产品类别。
    猜你喜欢
    • 2019-01-12
    • 2021-08-24
    • 2017-12-10
    • 2018-07-11
    • 2018-07-31
    • 2021-11-27
    • 1970-01-01
    • 2020-09-21
    • 2019-02-22
    相关资源
    最近更新 更多