WooCommerce中特定产品类别的最小购物车数量

psivel

在WooCommerce中,我对正在销售的产品使用“出口”类别,并且我想为购买任何“出口”产品的客户设置最低小计(30欧元)。

我试图陷入woocommerce_after_calculate_totals

  • 检查特定产品类别的购物车项目
  • 找到特定产品类别且订单价格低于30欧元时显示通知
  • 并最终在用户尝试以低于30€的价格结帐时重定向到购物车页面。

这是我的代码:

add_action( 'woocommerce_after_calculate_totals', 'check_order_outlet_items', 10, 0 );

function check_order_outlet_items() {

    global $woocommerce;

    if (is_cart() || is_checkout()) {

        // Check if cart contains items in Outlet cat.

        $items = $woocommerce->cart->get_cart();

        foreach($items as $item => $values) {

            $product_id = $values['product_id'];

            $terms = get_the_terms( $product_id, 'product_cat' );

            foreach ($terms as $term) {
                if ($term->name == "OUTLET") {
                    $outlet_found = 1;
                    break;
                }
            }
            if ($outlet_found) {break;}

        }

        if ($outlet_found) {

            // Calculate order amount including discount

            $cart_subtotal = $woocommerce->cart->subtotal;
            $discount_excl_tax_total = $woocommerce->cart->get_cart_discount_total();
            $discount_tax_total = $woocommerce->cart->get_cart_discount_tax_total();
            $discount_total = $discount_excl_tax_total + $discount_tax_total;
            $order_net_amount = $cart_subtotal - $discount_total;

            // Check if condition met

            if ($order_net_amount < 30) {

                if (is_checkout()) {

                    wp_redirect(WC()->cart->get_cart_url());
                    exit();

                } else {

                    wc_add_notice( __( 'You must order at least 30 €', 'error' ) );

                }
            }
        }
    }
}

此代码可在购物车页面中完美运行(即使添加优惠券后,即使购物车金额低于30,也显示购物车金额<30时会显示通知),并在用户想要结帐时重定向到购物车。

但是,如果我转到金额> = 30的结帐页面,然后添加优惠券(将购物车中的金额降低到30以下),则Ajax重新计算总计循环,该页面被阻止。但是,如果我重新加载了结帐页面,则可以正确地重定向到购物车页面。

LoicTheAztec

在这种情况下要使用的右钩子是woocommerce_check_cart_items这样的:

add_action( 'woocommerce_check_cart_items', 'check_cart_outlet_items' );
function check_cart_outlet_items() {
    $categories = array('OUTLET'); // Defined targeted product categories
    $threshold  = 30; // Defined threshold amount

    $cart       = WC()->cart;
    $cart_items = $cart->get_cart();
    $subtotal   = $cart->subtotal;
    $subtotal  -= $cart->get_cart_discount_total() + $cart->get_cart_discount_tax_total();
    $found      = false;

    foreach( $cart_items as $cart_item_key => $cart_item ) {
        // Check for specific product categories
        if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
            $found = true; // A category is found
            break; // Stop the loop
        }
    }

    if ( $found && $subtotal < $threshold ) {
        // Display an error notice (and avoid checkout)
        wc_add_notice( sprintf( __( "You must order at least %s" ), wc_price($threshold) ), 'error' );
    }
}

代码进入您的活动子主题(或活动主题)的functions.php文件中。经过测试和工作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

WooCommerce中特定产品类别的最小购物车项目数量

删除Woocommerce中针对特定产品类别的添加购物车按钮

更改 Woocommerce 中特定产品类别的购物车项目价格

为特定WooCommerce产品类别的购物车项目设置最小数量

隐藏 WooCommerce 购物车中特定产品类别的 woocommerce-checkout-review-order 表

更改Woocommerce 3中特定产品类别的“添加到购物车”文本

删除WooCommerce 3中特定产品类别的“添加到购物车”按钮

隐藏Woocommerce 3中特定产品类别的购物车优惠券字段

Ajax上的JS警报已添加到购物车,以获取Woocommerce中特定产品类别的数量

避免在Woocommerce中更新特定产品类别的购物车价格

计算特定产品类别的购物车项目

在WooCommerce的末尾对特定产品类别购物车项目进行排序

在WooCommerce购物车项目上为特定产品类别添加正文类

如果用户未登录Woocommerce,请避免将其添加到特定产品类别的购物车中

Woocommerce购物车中的特定产品类别时,有条件地应用CSS样式

在WooCommerce中为特定产品类别自定义“添加到购物车”按钮

根据WooCommerce产品类别禁用特定的购物车项目数量字段

隐藏购物车中“ WooCommerce”产品类别的“删除商品”

Woocommerce中基于状态和产品类别的累进购物车项目费用

当购物车中有特定产品ID时,设置Woocommerce产品类别商品价格

获取Woocommerce中特定产品类别的特定产品属性值

WooCommerce无法从产品类别访问购物车

Woocommerce 3中按产品类别的自定义购物车项目计数

更改特定产品类别的WooCommerce产品数量设置

禁用Woocommerce中特定类别的购物车商品的其他产品类别

如果购物车包含特定的Woocommerce产品类别,请阻止添加到购物车

WooCommerce特定产品类别的渐进式数量折扣

WooCommerce根据产品类别替换购物车/结帐中的“按需订购”

在Woocommerce中检查购物车项目中的产品类别