繁体   English   中英

WooCommerce中基于用户角色和类别的最小数量订购规则

[英]Minimum quantity order rules in WooCommerce based on user role and categories

我正在尝试构建 WooCommerce 购物车规则功能。 它应该像这样工作:

当用户的角色是“批发商”时,他们必须将相同产品类别的至少 2 或 3 件商品(取决于类别)添加到购物车中。 一旦满足该条件,无论之前设置的规则如何,他们都可以将任何产品添加到购物车中。

例如:

  • 袜子的最低订购量:3
  • 帽子的最低订购量:3
  • 衬衫起订量:2

场景:

  • 如果客户至少添加了 3 顶帽子,则应避免其他两个最低规则。
  • 如果客户添加 1 只袜子和 2 顶帽子,他们将无法完成订单,除非他们再添加 2 只袜子或 1 顶帽子。

基于防止 WooCommerce 结帐如果未达到类别的最小数量,除非添加另一个类别答案代码,这是我的代码尝试:

function action_woocommerce_check_cart_items() {
    // Only run on the cart or checkout pages
    if ( is_cart() || is_checkout() ) {     
        // Minimum
        $minimum = 3;
        
        // Category
        $category1 = 'socks';
        $category2 = 'hats';
        
        // Initialize
        $total_socks = 0;
        $total_hats = 0;
        
        // Loop through cart items        
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {        
            // Product id
            $product_id = $cart_item['product_id'];

            // Has certain category
            if ( has_term( $category1, 'product_cat', $product_id ) ) {              
                // Add to total
                $total_socks += $cart_item['quantity'];
            }
            
            elseif ( has_term( $category2, 'product_cat', $product_id ) ) {              
                // Add to total
                $total_hats += $cart_item['quantity'];
            }
        }
        

        // When total is greater than 0 but less than the minimum
        if ( ($total_socks > 0 && $total_socks < $minimum) && ( $total_hats > 0 && $total_hats < $minimum )  ) {
            // Notice
            wc_add_notice( sprintf( __( 'A minimum of %s products are required from the %s category before checking out.', 'woocommerce' ), $minimum, $category1 ), 'error' );
            
            // Optional: remove proceed to checkout button
            remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
        }
        
        
        
    }
}
add_action( 'woocommerce_check_cart_items' , 'action_woocommerce_check_cart_items', 10, 0 );

但是我无法添加用户角色检查并与脚本中缺少的其他类别(袜子和衬衫)混合,有什么建议吗?

首先,您可以使用current_user_can() function 来检查当前用户是否具有指定的能力。

然后你最好使用一个$settings数组,你将循环遍历多个 if/else 条件,因为这更有效且使用时间短得多。

通过$settings数组你可以设置"category""minimun""total"不应该被调整

可以通过不同的方式生成错误消息,但再次使用循环来自动创建消息。

所以你得到:

function action_woocommerce_check_cart_items() {
    // Check if the user has a role of wholesaler
    if ( ! current_user_can( 'wholesaler' ) ) return;

    // Settings (multiple settings arrays can be added/removed if desired)
    $settings = array(
        array(
            'category'  => 'socks',
            'minimum'   => 3,
            'total'     => 0
        ),
        array(
            'category'  => 'hats',
            'minimum'   => 3,
            'total'     => 0
        ),
        array(
            'category'  => 'shirts',
            'minimum'   => 2,
            'total'     => 0
        )
    );

    // Initialize
    $flag = false;

    // Collect data - loop through cart items        
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        // Get product ID
        $product_id = $cart_item['product_id'];

        // Get quantity
        $product_quantity = $cart_item['quantity'];

        // Loop trough settings array
        foreach ( $settings as $key => $setting ) {
            // Checks if the current product has any of given terms
            if ( has_term( $setting['category'], 'product_cat', $product_id ) ) {
                // Add to the total
                $settings[$key]['total'] += $product_quantity;
            }

            // Checks if the current total is equal to or greater than the minimum
            if ( $setting['total'] >= $setting['minimum'] ) {
                // Make true, break loop
                $flag = true;
                break;
            }
        }
    }

    // NOT true
    if ( ! $flag ) {
        // Initialize
        $message = '';
        $first_letter = __( 'A ', 'woocommerce' );

        // Generate error messages - loop trough settings array
        foreach ( $settings as $key => $setting ) {
            // NOT the first iteration in a foreach loop, convert to capital letter
            if ( $key !== array_key_first( $settings ) ) {
                // Make a string lowercase
                $first_letter = strtolower( $first_letter );
            }

            // Generate message, append
            $message .= sprintf( __( '%s minimum of %s products are required from the "%s" category', 'woocommerce' ), $first_letter, $setting['minimum'], $setting['category'] );

            // NOT the last iteration in a foreach loop, append 'OR'
            if ( $key !== array_key_last( $settings ) ) {
                $message .= '<strong>' . __( ' OR ', 'woocommerce' ) . '</strong>';
            }
        }

        // Append to message
        $message .= __( ' before checking out.', 'woocommerce' );

        // Notice
        wc_add_notice( $message, 'error' );

        // Removing the proceed button, until the condition is met
        remove_action( 'woocommerce_proceed_to_checkout','woocommerce_button_proceed_to_checkout', 20 );
    }
}
add_action( 'woocommerce_check_cart_items', 'action_woocommerce_check_cart_items', 10 );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM