Woocommerce 中的渐进式最小购物车商品数量计数限制

Progressive minimum cart items quantity count limitation in Woocommerce

在 Woocommerce 中,我想设置一些要求在客户能够结帐之前获得批准。我希望客户在结帐之前拥有 6 个数量。
数量不仅仅与一种产品有关,他们可以组合更多产品直到达到总数量。
如果恰好有 6 个,则可以继续。下一阶段,必须恰好有 12 个数量才能结帐(例如,如果购物车中有 8 个数量),然后是 18、24 等....

我正在使用这个功能,它只适用于 6 件物品的数量限制。我也想用之前解释的逻辑扩展功能,使其动态和渐进。

我的实际代码:

// Set a minimum number of products requirement before checking out
add_action( 'woocommerce_check_cart_items', 'spyr_set_min_num_products' );
function spyr_set_min_num_products() {
    // Only run in the Cart or Checkout pages
    if( is_cart() || is_checkout() ) {
        global $woocommerce;

        // Set the minimum number of products before checking out
        $minimum_num_products = 6;
        // Get the Cart's total number of products
        $cart_num_products = WC()->cart->cart_contents_count;

        // Compare values and add an error is Cart's total number of products
        // happens to be less than the minimum required before checking out.
        // Will display a message along the lines of
        // A Minimum of 20 products is required before checking out. (Cont. below)
        // Current number of items in the cart: 6   
        if( $cart_num_products < $minimum_num_products ) {
            // Display our error message
            wc_add_notice( sprintf( '<strong>A Minimum of %s products is required before checking out.</strong>' 
                . '<br />Current number of items in the cart: %s.',
                $minimum_num_products,
                $cart_num_products ),
            'error' );
        }
    }
}

任何帮助将不胜感激。

这里是让用户在订单状态改变时计算动态累进项目数量限制的方法,增加步骤基于数字六 (6)。

此外,数量必须是六 (6) 的倍数。

代码:

// User dynamic progressive item quantity count limitation
add_action( 'woocommerce_check_cart_items', 'user_dynamic_progressive_min_item_qty_count_limitation' );
function user_dynamic_progressive_min_item_qty_count_limitation() {
    // Only in the Cart or Checkout pages
    if( ! ( is_cart() || is_checkout() ) ) return; // Exit

    $base_step = 6; // The quantity step base
    $user_id   = get_current_user_id(); // User ID
    $min_qty   = get_user_meta( $user_id, '_min_item_qty_ref', true ); // Get min qty from user data
    $min_qty   = $user_id == 0 ? $base_step : ( empty($min_qty) ? $base_step : $min_qty );
    $qty_count = (int) WC()->cart->get_cart_contents_count(); // cart items count

    if( $qty_count < $min_qty ):

    // Display an error notice when quantity count is below the minimum
    wc_add_notice( sprintf( '<strong>A Minimum of %s products is required before checking out.</strong>'
        . '<br />Current number of items in the cart: %s.', $min_qty, $qty_count ), 'error' );

    elseif( ( $qty_count % 6 ) != 0 ):

    // Display an error notice when quantity count is not a multiple of 6
    wc_add_notice( sprintf( '<strong>A Multiple of %s products is required before checking out.</strong>'
        . '<br />Current number of items in the cart: %s.', $base_step, $qty_count ), 'error' );

    endif;
}

// User dynamic progressive item quantity count calculation (increase or decrease) on order status change
add_action('woocommerce_order_status_changed', 'user_dynamic_progressive_min_item_qty_calculation', 50, 4 );
function user_dynamic_progressive_min_item_qty_calculation( $order_id, $old_status, $new_status, $order ){

    $statuses_increase_limit = array('on-hold', 'processing', 'completed'); // Succesful statuses
    $statuses_decrease_limit = array('cancelled', 'failed'); // Negative statuses (decreasing)

    $min_qty_step = 6; // The quantity step base
    $customer_id  = (int) $order->get_customer_id(); // User ID
    $user_min_qty = (int) get_user_meta( $customer_id, '_min_item_qty_ref', true ); // Get min qty from user data
    $order_flag   = get_post_meta( $order_id, '_min_item_qty_flag', true ); // Get order min qty flag
    $order_flag   = empty($order_flag) ? false : $order_flag;

    if ( in_array($new_status, $statuses_increase_limit) && ! $order_flag )
    {
        $user_min_qty = $user_min_qty == 0 ? $min_qty_step : $user_min_qty;

        update_post_meta( $order_id, '_min_item_qty_flag', true );
        update_user_meta( $customer_id, '_min_item_qty_ref', $user_min_qty + $min_qty_step  ); // Increase
    }
    elseif ( in_array($new_status, $statuses_increase_limit) && $order_flag )
    {
        update_post_meta( $order_id, '_min_item_qty_flag', false );
        update_user_meta( $customer_id, '_min_item_qty_ref', $user_min_qty - $min_qty_step ); // Decrease
    }
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。

When an order is placed, the calculation increase the min items quantity limitation count and is set in user data. The the order is flagged. If the order is cancelled or failed, it will check for the order flag and if the flag is set it will decrease the min items quantity limitation count.