久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

WooCommerce 中特定產品類別的最小購物車項目數量

Minimum cart item quantity for specific product categories in WooCommerce(WooCommerce 中特定產品類別的最小購物車項目數量)
本文介紹了WooCommerce 中特定產品類別的最小購物車項目數量的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

在 WooCommerce 中,我需要為產品類別的每個項目設置最小數量.我搜索了論壇,發現了一些運行良好的代碼,但它只計算產品類別的總數量:

In WooCommerce, I need to set up a minimum quantity for each item of a product category. I searched the forum and found some code that works fine except it only counts the Quantity for a product category in total:

add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
function wc_minimum_order_amount() {
    $minimum = 5; //Qty product

    if ( WC()->cart->cart_contents_count < $minimum ) {
        $draught_links = array();

        foreach(WC()->cart->get_cart() as $cart_item_key => $values ) {
            $_product = $values['data'];

            $terms = get_the_terms( $_product->id, 'product_cat' );

            foreach ($terms as $term) {
                $draught_links[] = $term->name;
            }   
        }

        if (in_array("Noten", $draught_links)){
            $on_draught = true;
        }else{
            $on_draught = false;
        }

        if( is_cart() ) {
            if($on_draught){
                wc_print_notice( 
                    sprintf( 'Bitte beachte die Mindestbestellmenge. Du brauchst mindestens %s Notenexemplare pro Arrangement. Aktuell hast du %s Stück in deinem Warenkorb.' , 
                         $minimum , 
                         WC()->cart->cart_contents_count
                    ), 'error' 
                );
            }
        } else {
            if($on_draught){
                wc_add_notice( 
                    sprintf( 'Bitte beachte die Mindestbestellmenge. Du brauchst mindestens %s Notenexemplare pro Arrangement. Aktuell hast du %s Stück in deinem Warenkorb.' , 
                        $minimum , 
                        WC()->cart->cart_contents_count
                    ), 'error' 
                );
            }
        }
    }
}

例如,如果我有兩個屬于同一產品類別的產品(A 和 B)并將此類別的最小數量設置為 5,則在這種情況下不會出現客戶的錯誤消息:

For example if I have two products (A and B) of belonging to the same product category and set up minimum quantity for this category to 5, the error message for the customer won't appear in this case:

  • 產品 A:3
  • 產品 B:2

對于該類別的每個產品,我至少需要 5 個.

I need a min quantity of 5 for every single product of that category.

您知道如何更改和優化以下代碼嗎?

Do you have an idea how to change and optimize the following code?

推薦答案

從 WooCommerce 3 開始,您的實際代碼已經過時且不方便……有多種方法:

Since WooCommerce 3, your actual code is outdated and not convenient… There is multiple ways:

1).最好的方法:在產品級別設置最小數量(針對產品類別):

1). The best way: Set up the minimum quantity at product level (for a product category):

// On single product pages
add_filter( 'woocommerce_quantity_input_args', 'min_qty_filter_callback', 20, 2 );
function min_qty_filter_callback( $args, $product ) {
    $categories = array('Noten'); // The targeted product category(ies)
    $min_qty    = 5; // The minimum product quantity

    $product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();

    if( has_term( $categories, 'product_cat', $product_id ) ){
        $args['min_value'] = $min_qty;
    }
    return $args;
}

// On shop and archives pages
add_filter( 'woocommerce_loop_add_to_cart_args', 'min_qty_loop_add_to_cart_args', 10, 2 );
function min_qty_loop_add_to_cart_args( $args, $product ) {
    $categories = array('Noten'); // The targeted product category
    $min_qty    = 5; // The minimum product quantity

    $product_id = $product->get_id();

    if( has_term( $categories, 'product_cat', $product_id ) ){
        $args['quantity'] = $min_qty;
    }
    return $args;
}

代碼位于活動子主題(或活動主題)的 functions.php 文件中.經過測試和工作.

Code goes in functions.php file of your active child theme (or active theme). Tested and work.

2).替代方法:檢查購物車商品并顯示錯誤消息(類似于您的代碼):

2). Alternative way: Checking cart items and displaying an error message (similar to your code):

add_action( 'woocommerce_check_cart_items', 'wc_min_item_required_qty' );
function wc_min_item_required_qty() {
    $categories    = array('Noten'); // The targeted product category
    $min_item_qty  = 5; // Minimum Qty required (for each item)
    $display_error = false; // Initializing

    // Loop through cart items
    foreach(WC()->cart->get_cart() as $cart_item ) {
        $item_quantity = $cart_item['quantity']; // Cart item quantity
        $product_id    = $cart_item['product_id']; // The product ID

        // For cart items remaining to "Noten" producct category
        if( has_term( $categories, 'product_cat', $product_id ) && $item_quantity < $min_item_qty ) {
            wc_clear_notices(); // Clear all other notices

            // Add an error notice (and avoid checkout).
            wc_add_notice( sprintf( 'Bitte beachte die Mindestbestellmenge. Du brauchst mindestens %s Notenexemplare pro Arrangement. Aktuell hast du %s Stück in deinem Warenkorb.', $min_item_qty , $item_quantity ), 'error' );
            break; // Stop the loop
        }
    }
}

代碼位于活動子主題(或活動主題)的 functions.php 文件中.經過測試和工作.

Code goes in functions.php file of your active child theme (or active theme). Tested and work.

為了使其也適用于父產品類別,您還將添加此自定義功能:

To make it work for parent product category too, you will also add this custom function:

// Custom conditional function that handle parent product categories too
function has_product_categories( $categories, $product_id = 0 ) {
    $parent_term_ids = $categories_ids = array(); // Initializing
    $taxonomy        = 'product_cat';
    $product_id      = $product_id == 0 ? get_the_id() : $product_id;

    if( is_string( $categories ) ) {
        $categories = (array) $categories; // Convert string to array
    }

    // Convert categories term names and slugs to categories term ids
    foreach ( $categories as $category ){
        $result = (array) term_exists( $category, $taxonomy );
        if ( ! empty( $result ) ) {
            $categories_ids[] = reset($result);
        }
    }

    // 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;
}

代碼位于活動子主題(或活動主題)的 functions.php 文件中.經過測試和工作.

Code goes in functions.php file of your active child theme (or active theme). Tested and work.

然后在現有代碼中,您將替換:

Then in the existing code, you will replace:

has_term( $category, 'product_cat', $product_id )

has_product_categories( $category, $product_id )

這也將允許您處理父產品類別.

That will allow you to handle parent product categories too.

這篇關于WooCommerce 中特定產品類別的最小購物車項目數量的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產品的總訂單數)
Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和電話字段驗證問題中添加自定義注冊字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡單產品中添加一個將更改價格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 在线高清免费观看视频 | 日本一区二区三区在线观看 | 欧美视频一级 | 亚洲综合久久久 | 超碰在线人人 | 久久激情av| 99久久久久久久久 | 国产精品黄视频 | 97国产精品视频人人做人人爱 | 日韩乱码在线 | 国产最新网址 | 亚洲有码转帖 | 一区二区三区四区视频 | 久久国产精品视频免费看 | 天天操天天射天天 | 成人福利网站 | 亚洲成人在线免费 | 天天操操操操操 | 中文字幕亚洲国产 | 97免费在线视频 | 欧美精品一区在线 | 免费黄色片在线观看 | 久草影视在线 | 91精品91久久久| 欧美日韩高清一区二区三区 | wwwww在线观看 | 国产一区二区三区在线免费观看 | 国产一区高清 | 欧美一级久久久猛烈a大片 日韩av免费在线观看 | 亚洲国产精品久久久 | 亚洲毛片在线观看 | 国产目拍亚洲精品99久久精品 | 欧美日韩精品一区二区三区四区 | 欧美精品一区二区三区在线 | 欧美在线看片 | 成人在线网| 日韩av成人在线 | 中文字幕在线一区二区三区 | 亚洲精品大片 | 黑人性hd| 超碰在线免费公开 |