【问题标题】:Woocommerce Order Quantity in MultiplesWoocommerce 订单数量的倍数
【发布时间】:2013-11-24 14:34:23
【问题描述】:

我正在尝试将 woocommerce 限制为仅以 5、10 或 15 个的固定数量销售。 下面的代码 sn-p(我在这个论坛上找到的)允许我将最小数量设置为 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 ) );
 }
}

【问题讨论】:

  • WooCommerce 高级产品数量允许您设置步长值,即如果您将其设置为 10,那么客户将只能将 100 件商品或 110 件商品添加到购物车中。下一个有效订单数量将是 120,依此类推。对于任何想知道的人来说,当您用板条箱运送物品时,这很有用,每个箱子里有几十件物品。

标签: woocommerce


【解决方案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 ) );
}

【讨论】:

    【解决方案2】:

    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' );
    }
    ?>
    

    【讨论】:

    • 我发现这个答案很有帮助。我不需要第二个动作(与运输类有关),但很高兴知道如何去做。这是现代答案,应该是公认的答案,IMO。
    【解决方案3】:

    未经测试,但这应该可以,基本上只是将不同的最小值添加到数组中,因此 $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 ) );
     }
    }
    

    【讨论】:

    • 感谢您的回复,但不幸的是,这似乎不起作用,它实际上会导致整个网站无法加载。我通过一个 php 检查器运行它,它在第 4 行的代码中显示“PHP 语法检查:解析错误:语法错误,意外 T_LNUMBER,期待 ')' $minimum = array('5,'10','15') ;"
    • 好的,仔细检查后,我注意到数组中的 5 后面缺少一个 ',所以现在允许网站加载,但不幸的是,该功能实际上并没有工作。无论我有 5 件还是 10 件,它都会显示警告,它仍然会阻止结帐。
    【解决方案4】:

    尝试使用 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-16
      • 1970-01-01
      • 2015-08-10
      • 1970-01-01
      • 2019-08-31
      • 1970-01-01
      相关资源
      最近更新 更多