복붙노트

[WORDPRESS] Woocommerce에서 특정 카테고리의 제품 "X"의 배수로 설정 품목 수량

WORDPRESS

Woocommerce에서 특정 카테고리의 제품 "X"의 배수로 설정 품목 수량

해결법


  1. 1.업데이트 (도 상위 제품 카테고리 확장)

    업데이트 (도 상위 제품 카테고리 확장)

    단지 특정 제품 범주에 대한 당신의 코드가 작동 할 것입니다 다음을 시도해보십시오

    // Custom conditional function that checks also for parent product categories
    function has_product_category( $product_id, $category_ids ) {
        $term_ids = array(); // Initializing
    
        // Loop through the current product category terms to get only parent main category term
        foreach( get_the_terms( $product_id, 'product_cat' ) as $term ){
            if( $term->parent > 0 ){
                $term_ids[] = $term->parent; // Set the parent product category
                $term_ids[] = $term->term_id;
            } else {
                $term_ids[] = $term->term_id;
            }
        }
        return array_intersect( $category_ids, array_unique($term_ids) );
    }
    
    
    add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities' );
    function woocommerce_check_cart_quantities() {
        $multiples = 6;
        $total_products = 0;
        $category_ids = array( 35 );
        $found = false;
    
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            if ( has_product_category( $cart_item['product_id'], $category_ids ) ) {
                $total_products += $cart_item['quantity'];
                $found = true;
            }
        }
        if ( ( $total_products % $multiples ) > 0 && $found )
            wc_add_notice( sprintf( __('You need to buy in quantities of %s products', 'woocommerce'), $multiples ), 'error' );
    }
    

    코드 활성 자식 테마 (활성 테마)의 function.php 파일에 간다. 테스트 및 작동합니다.

  2. from https://stackoverflow.com/questions/52612495/set-item-quantity-to-multiples-of-x-for-products-in-a-specific-category-in-woo by cc-by-sa and MIT license