首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >强制仅在WooCommerce结帐时购买预定义的数量

强制仅在WooCommerce结帐时购买预定义的数量
EN

Stack Overflow用户
提问于 2020-02-15 11:55:06
回答 1查看 122关注 0票数 0

我用WooCommerce制作的WooCommerce卖12顿现成的饭菜。顾客也只能购买每种类型的一餐。但它必须达到以下精确之和:

5件,12件,24件,36件。

为什么?

准备好的食物是用恒温箱运送的,里面有精确数量的即食。

有四种类型的等温盒,它们包含:

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

因此,我不得不在购物车页面的结账处强制购买5、12、24或36顿饭。

这个数字也可以通过添加2餐A+3餐B来达到。

重要的是,例如,如果他们试图购买63A+ 3B产品,结账按钮就消失了,报警器就会显示:

你只能购买5,12,24,36顿饭。

EN

回答 1

Stack Overflow用户

发布于 2020-02-15 12:52:58

代码语言:javascript
运行
复制
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' );
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60238522

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档