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

限制購物車項目來自 WooCommerce 中的同一產(chǎn)品類別

Restricting cart items to be from the same product category in WooCommerce(限制購物車項目來自 WooCommerce 中的同一產(chǎn)品類別)
本文介紹了限制購物車項目來自 WooCommerce 中的同一產(chǎn)品類別的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時送ChatGPT賬號..

當購物車中添加了具有特殊產(chǎn)品類別cat_x"的項目并顯示一些不同的自定義通知時,我使用下面的代碼從購物車中刪除其他 WooCommerce 產(chǎn)品類別項目.代碼來自這個線程并且可以正常工作嗯:

I am using the code below to remove other WooCommerce product category items from the cart when there is an item with a special product category 'cat_x' added in cart and display some different custom notices. The code came from this thread and just works well:

add_action( 'woocommerce_check_cart_items', 'checking_cart_items' );
function checking_cart_items() {
    $special = false;
    $catx = 'cat_x';
    $number_of_items = sizeof( WC()->cart->get_cart() );

    if ( $number_of_items > 0 ) {

        // Loop through all cart products
        foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
            $item = $values['data'];
            $item_id = $item->id;

            // detecting if 'cat_x' item is in cart
            if ( has_term( $catx, 'product_cat', $item_id ) ) {
                if (!$special)
                    $special = true;
            }
        }

        // Re-loop through all cart products
        foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
            $item = $values['data'];
            $item_id = $item->id;

            if ( $special ) // there is a 'cat_x' item in cart
            { 
                if ( $number_of_items == 1 ) { // only one 'cat_x' item in cart
                    if ( empty( $notice ) )
                        $notice = '1';
                }
                if ( $number_of_items >= 2 ) { // 'cat_x' item + other categories items in cart
            // removing other categories items from cart
                    if ( !has_term( $catx, 'product_cat', $item_id ) ) {
                        WC()->cart->remove_cart_item( $cart_item_key ); // removing item from cart
                        if ( empty( $notice ) || $notice == '1' )
                            $notice = '2';
                    }
                }
            } else { // Only other categories items
                if ( empty( $notice ) )
                    $notice = '3';
            }
        }

        // Firing notices
        if ( $notice == '1' ) { // message for an 'cat_x' item only (alone)
            wc_add_notice( sprintf( '<p class="woocommerce-error">bla bla bla one category X item in the cart</p>' ), 'success' );
        } elseif ( $notice == '2' ) { // message for an 'cat_x' item and other ones => removed other ones 
            wc_add_notice( sprintf( '<p class="woocommerce-error">bla bla bla ther is already category X in the cart => Other category items has been removed</p>' ), 'error' );
        } elseif ( $notice == '3' ) { // message for other categories items (if needed)
            wc_add_notice( sprintf( '<p class="woocommerce-error">bla bla bla NOT category X in the cart</p>' ), 'success' );
        }
    }
} 

有條件函數(shù) has_term() 也適用于類別數(shù)組,我嘗試在該代碼中設(shè)置類別數(shù)組而不是一個類別.但它不起作用.

Has the conditional function has_term() works also with arrays of categories, I have tried instead of one category, to set an array of categories in that code. But it’s not working.

但是,我的需求發(fā)生了變化:我不想讓客戶有可能從不同類別中選擇購物車項目.因此,購物車必須始終包含來自同一產(chǎn)品類別的商品.

However, my needs have changed: I don’t want to let the customer have the possibility to select cart items from different categories. So the cart must always have items from the same product category.

有什么幫助嗎?

謝謝.

推薦答案

制作一個掛在 woocommerce_add_to_cart_validation 過濾器鉤子中的自定義函數(shù)將以更簡單的方式完成這項工作,無需設(shè)置類別數(shù)組.

Making a custom function hooked in woocommerce_add_to_cart_validation filter hook is going to do the job in a much more simpler way, without any need to set an array of categories.

所以你的代碼會更快更緊湊.此外,您還可以顯示自定義通知以警告客戶.

So your code will be much more faster and compact. Additionally you can display a custom notice to warn the customer.

如果購物車中有不同類別的商品,此代碼將避免添加到購物車:

This code will avoid adding to cart, if an item of a different category is in cart:

add_filter( 'woocommerce_add_to_cart_validation', 'add_to_cart_validation_callback', 10, 3 );
function add_to_cart_validation_callback( $passed, $product_id, $quantity) {
    // HERE set your alert text message
    $message = __( 'MY ALERT MESSAGE.', 'woocommerce' );
    
    if( ! WC()->cart->is_empty() ) {
        // Get the product category terms for the current product
        $terms_slugs = wp_get_post_terms( $product_id, 'product_cat', array('fields' => 'slugs'));
        
        // Loop through cart items
        foreach (WC()->cart->get_cart() as $cart_item ){
            if( ! has_term( $terms_slugs, 'product_cat', $cart_item['product_id'] )) {
                $passed = false;
                wc_add_notice( $message, 'error' );
                break;
            }
        }
    }
    return $passed;
}

代碼位于活動子主題(或主題)的 function.php 文件或任何插件文件中.

此代碼已經(jīng)過測試并且可以運行

This code is tested and it works

限制購物車中的商品只能來自不同的產(chǎn)品類別:

替換函數(shù)中的條件:

if( ! has_term( $product_cats, 'product_cat', $cart_item['product_id'] )) { 

if( has_term( $product_cats, 'product_cat', $cart_item['product_id'] )) {

這篇關(guān)于限制購物車項目來自 WooCommerce 中的同一產(chǎn)品類別的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產(chǎn)品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產(chǎn)品的總訂單數(shù))
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 簡單產(chǎn)品中添加一個將更改價格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產(chǎn)品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結(jié)帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 日日摸夜夜添夜夜添精品视频 | 伊人激情综合网 | 免费黄色a视频 | 国产视频一视频二 | 中国一级毛片免费 | 国产成人精品区一区二区不卡 | 日日做夜夜爽毛片麻豆 | 欧美高清视频在线观看 | 欧美精品导航 | 久久久久久久久久久久91 | 视频在线一区二区 | 欧美a视频| 久久99久久99精品免视看婷婷 | 国产中文字幕网 | 日本不卡一区 | 涩涩鲁亚洲精品一区二区 | 伊人免费观看视频 | a级毛片毛片免费观看久潮喷 | 成人午夜影院 | 欧美精品一区二区三区在线 | 日韩欧美在线视频一区 | 精品国产久 | 怡红院免费的全部视频 | 99精品久久久 | 久久久久国产一级毛片 | 麻豆久久久 | 亚洲欧美综合精品久久成人 | 美国黄色一级片 | 拍拍无遮挡人做人爱视频免费观看 | 国产又色又爽又黄又免费 | 国产美女黄色 | 欧美一级毛片免费观看 | 国产一区二区精品在线 | 日韩一区二区三区精品 | 中文在线观看视频 | 国产精品永久免费 | 一级免费毛片 | h网站在线观看 | 成人亚洲网站 | 黄色一级大片在线免费看产 | 九九热热九九 |