php - 在 Woocommerce 3 中隐藏购物车中特定产品的数量字段

标签 php wordpress woocommerce cart product-quantity

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

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

这可能吗?任何轨道都会受到赞赏。

最佳答案

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

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 文件。测试和工作。
它还将处理添加到购物车的产品变体。

关于php - 在 Woocommerce 3 中隐藏购物车中特定产品的数量字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51102798/

相关文章:

css - 我怎样才能让这个表单和按钮在响应式设计中正确调整大小?

php - PHP外部程序

php - 没有额外空格的 Laravel Blade?

php - 将键附加到现有数组值

jquery - 在 wordpress 移动设备上隐藏特定帖子

php - 如何更改 Woocommerce 结帐页面中的 "Have a coupon?"文本

php - 我怎样才能将数组与日期合并到一个php中

html - 旋转文本 -90 度并与 div 垂直对齐

php - 一种显示当前登录的 Wordpress 用户网站的 Google Analytics 数据的方法?

wordpress - 在 woocommerce 结帐时永久移动 301 错误