在woocommerce中,我使用的是Hide “remove item” from cart for WooCommerce product category应答代码,我也想禁用购物车数量字段,以避免客户将商品数量更改为零。
有可能吗?对此的任何跟踪将不胜感激。
答案 0 :(得分:1)
以下代码将从特定产品类别(您将在第二个功能中定义)的商品中删除购物车中的“数量字段”:
// Custom conditional function that checks for categories (including parent)
function has_product_categories( $product_id, $categories ) {
// Initializing
$parent_term_ids = $categories_ids = array();
$taxonomy = 'product_cat';
// Convert categories term names and slugs to categories term ids
foreach ( $categories as $category ){
if( is_numeric( $category ) ) {
$categories_ids[] = (int) $category;
} elseif( term_exists( sanitize_title( $category ), $taxonomy ) ) {
$categories_ids[] = get_term_by( 'slug', sanitize_title( $category ), $taxonomy )->term_id;
}
}
// Loop through the current product category terms to get only parent main category term
foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
if( $term->parent > 0 ){
$parent_term_ids[] = $term->parent; // Set the parent product category
$parent_term_ids[] = $term->term_id; // (and the child)
} else {
$parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.
}
}
return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
}
add_filter( 'woocommerce_quantity_input_args', 'hide_cart_quantity_input_field', 20, 2 );
function hide_cart_quantity_input_field( $args, $product ) {
// HERE your specific products categories
$categories = array( 'clothing' );
// Handling product variation
$product_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_product_categories( $product_id, $categories ) ){
$input_value = $args['input_value'];
$args['min_value'] = $args['max_value'] = $input_value;
}
return $args;
}
代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。
注意::如果您还使用其他答案代码,则第一个函数已经定义,并且在函数php文件中不必重复两次……