Скрыть «удалить товар» из корзины для категории товаров WooCommerce - PullRequest
0 голосов
/ 04 января 2019

Я хотел бы скрыть «удалить элемент» из корзины для определенной категории продуктов в WooCommerce, как и в « Скрыть» удалить элемент из корзины для определенного продукта в WooCommerce"Нить ответа, но для определенной категории продуктов.

Любая помощь будет оценена.

Ответы [ 2 ]

0 голосов
/ 04 января 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;
}

// Hiding "remove item" for specific product category
add_filter('woocommerce_cart_item_remove_link', 'filter_cart_item_remove_link', 20, 2 );
function filter_cart_item_remove_link( $button_link, $cart_item_key ){
    // HERE your specific products categories
    $categories = array( 'clothing' );

    // Get the current cart item
    $cart_item = WC()->cart->get_cart()[$cart_item_key];

    // If the targeted product is in cart we remove the button link
    if( has_product_categories( $cart_item['product_id'], $categories ) )
        $button_link = '';

    return $button_link;
}

Код помещается в файл function.php вашей активной дочерней темы (или активной темы).Проверено и работает.

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

Приведенный ниже код будет работать так, как вы хотите: просто измените идентификатор категории в , если предложение:

function tristup_woocommerce_cart_item_remove_link($link){
    preg_match('/data-product_id=\"(.*?)\"/', $link, $matches);

    $flag=0;
    if($matches)
    {
        $product_id=$matches[1];
        $terms = get_the_terms ( $product_id, 'product_cat' );
        foreach ( $terms as $term ) 
        {
            if($term->term_id=='210') // change with your category id or add more with in proper syntaxes
            {
                return '';              
            }
        }

    }  
    return $link;       
}
add_filter('woocommerce_cart_item_remove_link', 'tristup_woocommerce_cart_item_remove_link');
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...