【问题标题】:Remove specific products from WooCommerce cart under certain amount从 WooCommerce 购物车中删除特定数量的特定产品
【发布时间】:2021-11-27 09:06:28
【问题描述】:

我正在尝试根据Set minimum Order amount for specific Products or Categories in WooCommerce 答案代码,从购物车中总共 200 件以下的购物车中删除 2 件会员产品,这是脚本:

add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
 
function wc_minimum_order_amount() {
    $minimum_amount = 200;
    $category_ids   = array( 83 ); 
    $product_ids    = array( 1111 , 1112 ); 


    if( WC()->cart->is_empty() || ! ( is_cart() || is_checkout() ) ) 
        return; 

    $total_amount = WC()->cart->subtotal; // Items subtotal including taxes

    if ( $total_amount < $minimum_amount ) {
        $needs_minimum_amount = false; 

        // Loop through cart items
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            $product_id   = $cart_item['product_id'];
            $variation_id = $cart_item['variation_id'];
            
            if( sizeof($category_ids) > 0 ) {
                $taxonomy = 'product_cat';

                if ( has_term( $category_ids, $taxonomy, $product_id ) ) { 
                    $needs_minimum_amount = true;
                    break; // Stop the loop
                }
            }

            if( sizeof($product_ids) > 0 ) {
                if ( array_intersect( $product_ids, array($product_id, $variation_id) ) ) { 
                    $needs_minimum_amount = true;
                    break; 
                }
            }
        }

        if( $needs_minimum_amount ) {
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        if ( $cart_item['product_id'] == 1111 || $cart_item['product_id'] == 1112 ) {
            WC()->cart->remove_cart_item( $cart_item_key );
            }
    }   
            wc_print_notice( sprintf( 
                __("רכישת חברת מועדון מעל %s. כרגע סך העגלה שלך עומד על %s."), 
                wc_price( $minimum_amount ), 
                wc_price( $total_amount )
            ), 'error' );
    


        }
    }
}

由于某种原因,产品没有从购物车中删除,但通知有效,有人知道我错过了什么吗?

查看网站-> https://woocommerce-620267-2036098.cloudwaysapps.com/

【问题讨论】:

  • 您查看过您的日志文件了吗?这些是否包含错误消息?你提到删除产品。但是,也有一部分考虑了类别。这也适用吗?
  • 从我在网上看到的 remove_cart_item() 不需要分类
  • 那为什么在你的代码中使用它呢?如果不需要?
  • 它需要确定产品是否需要最低金额
  • 我只是不明白为什么 remove_cart_item() 不起作用:(

标签: php wordpress woocommerce cart


【解决方案1】:

还有一些错误和错误的钩子,我还为您简化了它,因为您只需要检查产品 ID:

add_action( 'woocommerce_before_checkout', 'bbloomer_minimum_order_amount' );
add_action( 'woocommerce_before_cart', 'bbloomer_minimum_order_amount' );
 
function bbloomer_minimum_order_amount() {
    $minimum_amount = 200;
    $product_ids = array( 186181, 186185 ); 
    if ( WC()->cart->is_empty() ) return; 
    $total_amount = WC()->cart->subtotal; // Items subtotal including taxes
    if ( $total_amount < $minimum_amount ) {
        $needs_minimum_amount = false; 
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            $product_id = $cart_item['product_id'];
            $variation_id = $cart_item['variation_id'];
            if ( array_intersect( $product_ids, array( $product_id, $variation_id ) ) ) { 
                $needs_minimum_amount = true;
                break; 
            }
        }
        if ( $needs_minimum_amount ) {
            foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
                if ( array_intersect( $product_ids, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
                    WC()->cart->remove_cart_item( $cart_item_key );
                }
            }
            wc_print_notice( sprintf( 
                __("רכישת חברת מועדון מעל %s. כרגע סך העגלה שלך עומד על %s."), 
                wc_price( $minimum_amount ), 
                wc_price( $total_amount )
            ), 'error' );
        }
    }
}

【讨论】:

  • 您好,感谢您的帮助,我需要从购物车中删除产品 1111 和 1112,但我不明白为什么它没有删除它们
  • 是的 - 您是否将这些 ID 放入 $product_ids 中?
猜你喜欢
  • 2021-01-26
  • 2017-12-10
  • 1970-01-01
  • 2018-07-11
  • 1970-01-01
  • 2018-07-31
  • 2020-09-21
  • 1970-01-01
  • 2017-05-30
相关资源
最近更新 更多