筛选以在结帐时检查数量 [英] Filter to check quantity at checkout

查看:19
本文介绍了筛选以在结帐时检查数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果可能,我想用 WooCommerce 实现一个特定的功能.有没有办法让 WooCommerce 检查购物车中的商品数量是否可以被 12 整除?

I have a specific function I would like to implement with WooCommerce if possible. Is there a way that I can have WooCommerce check if the quantity of items in the cart is divisible by 12?

基本上我有一个商店设置,但我需要客户订购 12 个(这是我运送物品的箱子的大小).但是,我有一些产品变体(尺寸、宽度等),我希望客户能够混合和匹配变体,以达到 12(或 24、36 等)的数量

Basically I have a shop setup but I need customers to order in quantities of 12 (that's the size of the boxes I ship the items in). However, I have a few product variations (size, width, etc.) and I want the customers to be able to mix and match variations in order to reach a quantity of 12 (or 24, 36, etc.)

我认为最简单的方法可能是安装一个功能来检查总购物车数量以及它是否可以被 12 整除.如果是,他们可以继续.如果没有,他们需要更改数量才能继续.有没有人知道是否有适合这种需求的插件或者是否有一些我可以添加的自定义代码?

I'm gathering that the easiest way to do this is probably to install a function that checks the total cart quantity and if it is divisible by 12. If so, they can proceed. If not, they need to change their quantity to continue. Does anyone have experience on if there is a plugin that suits this need or if there is some custom code I can add?

非常感谢任何帮助.

问候,瑞恩

推荐答案

这应该适合你:

将以下内容添加到functions.php文件中,在checkout时它会检查数量是否是12的倍数,如果是则很好,如果不是你得到错误信息您需要购买 12 件商品 并且无法继续.

Add the following to the functions.php file, at checkout it will check and see if the quantity is a multiple of 12, if it is then it's fine, if not you get the error message You need to buy in quantities of 12 products and won't be able to proceed.

add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities' );
function woocommerce_check_cart_quantities() 
{
    global $woocommerce;
    $multiples = 12;
    $total_products = 0;
    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) 
    {
        $total_products += $values['quantity'];
    }
    if ( ( $total_products % $multiples ) > 0 )
        $woocommerce->add_error( sprintf( __('You need to buy in quantities of %s products', 'woocommerce'), $multiples ) );
}

以上内容略有修改自这篇帖子.

The above is slightly modified from this post.

这篇关于筛选以在结帐时检查数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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