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

禁用 Woocommerce 中特定類別的購物車項(xiàng)目的其他產(chǎn)

Disable other product categories for a cart item from specific category in Woocommerce(禁用 Woocommerce 中特定類別的購物車項(xiàng)目的其他產(chǎn)品類別)
本文介紹了禁用 Woocommerce 中特定類別的購物車項(xiàng)目的其他產(chǎn)品類別的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時送ChatGPT賬號..

我正在開發(fā)一個網(wǎng)上商店,有不同的方面,第一個是普通商店,第二個是夜間啤酒服務(wù).我已經(jīng)搜索過但找不到我要找的東西;

I'm developing a webshop, with different aspects, the first is a regular shop, and the second is a night beer service. i've searched but can't find what i am looking for;

首先,夜間啤酒服務(wù)是一個特定類別,不應(yīng)與常規(guī)商品一起訂購(如果類別 'beerservice' 在購物車中,請禁用所有其他類別以添加到購物車).

First the night beer service is a specific category that shouldn't be ordered with the regular items (if category 'beerservice' is in the cart, disable all other categories to be added to the cart).

此選項(xiàng)也需要反之亦然(其他情況),因此如果將常規(guī)商品添加到購物車,則應(yīng)禁用 'beerservice' 類別以添加到購物車購物車.

This option also needs to work vice-versa (the other wat around), so if a regular item is added to the cart, the category of 'beerservice' should be disabled to add to the cart.

我正在使用我找到的這個答案代碼:
當(dāng)來自特定產(chǎn)品類別的商品在 Woocommerce 的購物車中時禁用購物

I am using this answer code that I have found:
Disable shopping when an item from a specific product category is in cart in Woocommerce

這部分完成了工作,但只能從同一類別添加多個產(chǎn)品,反之亦然.我已經(jīng)替換了這段代碼

That does the job partially, but it's only impossible to add multiple products from the same category, and is doesn't work vice-vera. I have replaced in this code

我還想添加一條特定于時間的消息,例如超過 TIME 訂購的訂單將不會送達(dá)"(沒有研究過這個),但如果不是很麻煩.

I also would like to add a time specific message like, 'Order ordered past TIME won't be delivered'(didn't research this) but if it isn't to much trouble.

推薦答案

此代碼將檢查父產(chǎn)品類別,因此對于定義的產(chǎn)品類別,它將:

This code will check for parent product categories, so for a defined product category, it will:

  • 當(dāng)定義的類別在購物車中時,避免將其他產(chǎn)品類別添加到購物車.
  • 將定義類別中的產(chǎn)品添加到購物車時,它將從其他產(chǎn)品類別中刪除購物車項(xiàng)目.

改進(jìn)的代碼:

// Custom conditional function that checks for parent product categories
function has_parent_term( $product_id ) {
    // HERE set your targeted product category SLUG
    $category_slug = 'beerservice'; //  <====  <====  <====  <====  <====  <====  <====

    // Convert category term slug to term id
    $category_id   = get_term_by('slug', $category_slug, 'product_cat')->term_id;
    $parent_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 ){
            $parent_term_ids[] = $term->parent; // Set the parent product category
        } else {
            $parent_term_ids[] = $term->term_id;
        }
    }
    return in_array( $category_id, $parent_term_ids );
}

// Avoid add to cart others product categories when "beerservice" is in cart
add_filter( 'woocommerce_add_to_cart_validation', 'specific_category_avoid_add_to_cart_others', 20, 3 );
function specific_category_avoid_add_to_cart_others( $passed, $product_id, $quantity) {
    if( WC()->cart->is_empty() || has_parent_term( $product_id ) ) {
        return $passed;
    }

    foreach( WC()->cart->get_cart() as $cart_item ){
        if( has_parent_term( $cart_item['product_id'] ) ) {
            wc_add_notice( __('Alert message 1 (avoid add to cart)', 'woocommerce' ), 'error' ); // Display a custom error notice
            return false; // Avoid add to cart
        }
    }
    return $passed;
}

// Remove other items when our specific product is added to cart
add_action( 'woocommerce_add_to_cart', 'conditionally_remove_other_products', 20, 4 );
function conditionally_remove_other_products ( $cart_item_key, $product_id, $quantity, $variation_id ){
    if( has_parent_term( $product_id ) ) {
        foreach( WC()->cart->get_cart() as $item_key => $cart_item ){
            if( ! has_parent_term( $cart_item['product_id'] ) ) {
                WC()->cart->remove_cart_item( $item_key );
                wc_add_notice( __('Alert message 2 (Item removed from cart)', 'woocommerce' ), 'error' ); // Display a custom error notice
            }
        }
    }
}

代碼位于您的活動子主題(活動主題)的 function.php 文件中.經(jīng)測試有效.

Code goes in function.php file of your active child theme (active theme). Tested and works.

相關(guān):當(dāng) Woocommerce 的購物車中有特定產(chǎn)品類別的商品時禁用購物

這篇關(guān)于禁用 Woocommerce 中特定類別的購物車項(xiàng)目的其他產(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 和電話字段驗(yàn)證問題中添加自定義注冊字段)
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)
主站蜘蛛池模板: 精区3d动漫一品二品精区 | 成人在线免费观看视频 | 精品国产精品国产偷麻豆 | 男女午夜免费视频 | 日韩一区二区在线播放 | 精品一区二区三区91 | 2018中文字幕第一页 | 美女在线观看av | 国产精品一区二区三区在线 | 在线免费观看一区二区 | 在线欧美一区二区 | 99re视频精品 | 狠狠的干| 欧美日韩在线精品 | 日本三级电影在线看 | 日韩欧美在线观看视频 | 亚洲人成在线观看 | 亚洲精品国产成人 | 精品av| 伊人导航 | 日韩精品一区二区三区视频播放 | 久久久久久国产精品三区 | 中文字幕高清av | 久久精品国产v日韩v亚洲 | 噜久寡妇噜噜久久寡妇 | 免费观看一级特黄欧美大片 | 国产成人高清在线观看 | 亚洲国产一区二区三区 | 91精品国产综合久久久亚洲 | 亚洲网视频| 天天干天天干 | av永久| 欧美伊人 | 日本爱爱 | 亚洲男人的天堂网站 | 国产成人久久精品一区二区三区 | 欧美中文字幕一区二区三区亚洲 | 91秦先生艺校小琴 | 久精品久久 | 成人av网站在线观看 | caoporn国产精品免费公开 |