gpt4 book ai didi

php - 禁用 Woocommerce 中特定类别的购物车项目的其他产品类别

转载 作者:行者123 更新时间:2023-12-04 17:44:51 24 4
gpt4 key购买 nike

我正在开发一个网上商店,有不同的方面,第一个是普通商店,第二个是夜间啤酒服务。我已经搜索过但找不到我要找的东西;

首先,夜间啤酒服务是一个特定类别,不应与常规商品一起订购(如果类别 'beerservice' 在购物车中,请禁用所有其他类别添加到购物车)。

此选项也需要反之亦然(另一个 wat 周围),因此如果将常规商品添加到购物车,类别 'beerservice'应禁用添加到购物车。

我正在使用我发现的这个答案代码:
Disable shopping when an item from a specific product category is in cart in Woocommerce

这部分完成了工作,但只能从同一类别添加多个产品,反之亦然。我已经替换了这段代码

我还想添加一个特定于时间的消息,比如“超过 TIME 订购的订单将不会被交付”(没有研究过这个)但如果它不是很麻烦。

最佳答案

此代码将检查父产品类别,因此对于定义的产品类别,它将:

  • 当定义的类别在购物车中时,避免将其他产品类别添加到购物车。
  • 当定义类别中的产品添加到购物车时,它将从其他产品类别中删除购物车项目。

  • 改进的代码:
    // 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 文件中。测试和工作。

    相关: Disable shopping when an item from a specific product category is in cart in Woocommerce

    关于php - 禁用 Woocommerce 中特定类别的购物车项目的其他产品类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52598329/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com