Multiples中的Woocommerce订单数量

时间:2013-11-24 14:34:24

标签: woocommerce

我试图将woocommerce限制为仅以5,10或15的定量出售。 下面的代码片段(我在这个论坛上找到)允许我设置最小数量5,但我想知道是否有人可以建议是否可以修改为允许5,10或15。

感谢您提供的任何帮助。

add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
function wc_minimum_order_amount() {
global $woocommerce;
$minimum = 5;
if ( $woocommerce->cart->cart_contents_count < $minimum ) {
    $woocommerce->add_error( sprintf( 'Cards can only be purchased in multiples of %s    Please ammend your order.' , $minimum ) );
 }
}

4 个答案:

答案 0 :(得分:1)

感谢Gareth的回答,但我实际上找到了一种更好的方式来实现我所需要的,只需发布​​以防任何其他人需要它。

我发现最好将购物车限制为以5的倍数出售,这可以通过以下代码实现。

// check that cart items quantities totals are in multiples of 5
add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities' );
function woocommerce_check_cart_quantities() {
  global $woocommerce;
  $multiples = 5;
    $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 ) );
}

答案 1 :(得分:1)

WooCommerce仅允许以倍数结账,强制客户在结账前将特定数量的倍数添加到购物车

<?php
// check that cart items quantities totals are in multiples of 6
add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities' );
function woocommerce_check_cart_quantities() {
    $multiples = 6;
    $total_products = 0;
    foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
        $total_products += $values['quantity'];
    }
    if ( ( $total_products % $multiples ) > 0 )
        wc_add_notice( sprintf( __('You need to buy in quantities of %s products', 'woocommerce'), $multiples ), 'error' );
}

// Limit cart items with a certain shipping class to be purchased in multiple only
add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities_for_class' );
function woocommerce_check_cart_quantities_for_class() {
    $multiples = 6;
    $class = 'bottle';
    $total_products = 0;
    foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
        $product = get_product( $values['product_id'] );
        if ( $product->get_shipping_class() == $class ) {
            $total_products += $values['quantity'];
        }
    }
    if ( ( $total_products % $multiples ) > 0 )
        wc_add_notice( sprintf( __('You need to purchase bottles in quantities of %s', 'woocommerce'), $multiples ), 'error' );
}
?>

答案 2 :(得分:0)

未经测试但这应该有效,基本上只是将不同的最小值添加到数组中,因此检查$ minimum将检查所有3个数量

add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
function wc_minimum_order_amount() {
global $woocommerce;
$minimum = array('5,'10','15');
if ( $woocommerce->cart->cart_contents_count < $minimum ) {
    $woocommerce->add_error( sprintf( 'Cards can only be purchased in multiples of %s    Please ammend your order.' , $minimum ) );
 }
}

答案 3 :(得分:-1)

尝试使用WooCommerce高级产品数量,它应该为您解决此问题。否则,以下功能是用于获取购物车和产品页面的正确步骤和最小值的功能。请记住,如果您只更改输入值,则必须在添加到购物车和结帐时验证它们(同样下面链接的插件会为您完成所有这些)。

http://wordpress.org/plugins/woocommerce-incremental-product-quantities/

否则此过滤器可能对购物车有所帮助:

add_filter( 'woocommerce_quantity_input_step', 'input_step_value', 1, 2);

function input_step_value( $default, $product ) {
     return 5;
}   

这是产品页面的一个:

add_filter( 'woocommerce_quantity_input_args', 'input_set_all_values', 1, 2 );

function input_set_all_values( $args, $product ) {
     $args['step'] = 5;     
     $args['min_value'] = 5;
     return $args;
}