【问题标题】:Disable other product categories for a cart item from specific category in Woocommerce禁用 Woocommerce 中特定类别的购物车项目的其他产品类别
【发布时间】:2020-08-09 07:12:39
【问题描述】:

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

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

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

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

这可以部分完成工作,但不可能添加来自同一类别的多个产品,反之亦然。我已经在这段代码中替换了

我还想添加一条特定于时间的消息,例如,“在 TIME 之后订购的订单将不会送达”(没有对此进行研究),但如果不是太麻烦的话。

【问题讨论】:

  • 请,当您使用现有答案中的代码时,请在您的问题中始终添加链接。如果您没有对代码进行真正的更改,请不要在您的问题中添加代码,只需保留链接即可。另请注意,在 StackOverFlow 中,您不能一次提出多个问题,因为您的问题会因为过于宽泛而被关闭。
  • 对不起!会记住的,谢谢提醒

标签: php wordpress woocommerce product custom-taxonomy


【解决方案1】:

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

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

改进的代码:

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

【讨论】:

  • 能否请您帮我如何使用相同的代码但使用类别术语?例如,您有产品属性颜色和术语:红色、黑色、白色。我不知道如何访问“红色”术语并在您的示例中使用它而不是类别 slug 'beerservice'。非常感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2019-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-10
  • 2019-06-15
  • 1970-01-01
  • 2018-07-10
相关资源
最近更新 更多