Отключить определенные поля количества товаров в корзине на основе категории продуктов WooCommerce - PullRequest
0 голосов
/ 05 января 2019

В woocommerce я использую Скрыть «удалить товар» из корзины для категории продуктов WooCommerce код ответа, и я также хотел бы отключить поле количества в корзине, чтобы клиент не мог изменить количество товара на ноль.

Это возможно? Любой трек на этом будет оценен.

1 Ответ

0 голосов
/ 05 января 2019

Следующий код удалит «поле количества» из корзины для товаров из определенной категории продуктов (которые вы определите во 2-й функции):

// 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 функции…

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...