强制在结帐时仅在WooCommerce中购买预定义的数量 [英] Force to buy only predefined quantities at checkout in WooCommerce

查看:89
本文介绍了强制在结帐时仅在WooCommerce中购买预定义的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 WooCommerce 制成的电子商务出售12顿便餐。
每种类型的顾客也只能购买一顿饭。但是它必须达到以下确切的总和:

My ecommerce made with WooCommerce sells 12 ready meals. The customer can also purchase only 1 meal per type. But it must necessarily reach the exact sum of:

5或12或24或36个产品。

5 or 12 or 24 or 36 products.

为什么?

即食食品放在装有精确数量即食食品的恒温箱中运送。

Ready meals are shipped in isothermal boxes that contain a precise number of ready meals.

恒温箱有4种类型,它们包含:

There are 4 types of isothermal boxes and they contain:


  • 5餐,12餐,24餐或36餐

所以我不得不强迫只买5个购物车页面上的结帐时有12、24或36顿饭。

So I have to force to buy only 5, 12, 24, or 36 meals at checkout on the cart page.

这个数字也可以通过添加例如2顿饭A + 3顿饭B

This figure can also be reached by adding for example 2 meals A + 3 meals B

重要的是,例如,如果他们尝试购买6种3A + 3B产品,则结帐按钮消失,并且出现警报,提示:

The important thing is that if, for example, they try to buy 6 3A + 3B products, the checkout button disappears and an alarm comes out that says:

您只能购买5,12,24,36餐。

You can only purchase 5,12,24,36 meals.

推荐答案

function checkout_validate() {
    // Only run in the Cart or Checkout pages
    if( is_cart() || is_checkout() ) {
        // Get number of items in the cart.
        $items_in_cart = WC()->cart->get_cart_contents_count();

        // Allowed amounts
        $allowed_amounts = array(5, 12, 24, 36);

        // If items in cart NOT equal to allowed amounts, show error message
        if ( !in_array( $items_in_cart, $allowed_amounts)) {
            wc_add_notice( __( 'Remember that you can only buy 5, 12, 24 or 36 meals. You have ' . $items_in_cart . ' maeals in your cart.', 'woocommerce' ), 'error' );
            remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
        }
    }
}   
add_action( 'woocommerce_check_cart_items' , 'checkout_validate' );

这篇关于强制在结帐时仅在WooCommerce中购买预定义的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆