簡體   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