簡體   English   中英

允許在 WooCommerce 中為特定用戶角色添加相同父類別的產品到購物車

[英]Allow add to cart products of the same parent category for specific user role in WooCommerce

我需要有關代碼的指導。 它目前對我來說工作正常。 代碼所做的是限制不同類別的產品組合,即一次只能將一個類別的產品添加到購物車中。

但是現在我需要僅在連接特定角色時才執行此代碼,在本例中為“特許經營”角色。 我修改了代碼,並將條件設置為僅適用於角色“特許經營”,但它不能正常工作,實際上限制了我在“特許經營”中的類別組合,但在其他角色中不允許我將產品添加到購物車並且不是我想要的,應該允許在現有的其他角色中正常添加,並組合來自不同類別的產品,假設您正在調整這些限制僅發生在“特許經營”中。

我會很感激你的幫助

這是我的代碼嘗試:

$user = wp_get_current_user();

if ( in_array( 'franquicia', (array) $user->roles ) ) {

    function avoid_add_to_cart_from_different_main_categories( $passed, $product_id, $quantity ) {

        
        $cart      = WC()->cart;
        $taxonomy  = 'product_cat';
        $ancestors = [];

        if( $cart->is_empty() )
             return $passed;

        $terms = (array) wp_get_post_terms( $product_id, $taxonomy, array('fields' => 'ids') );

        if( count($terms) > 0 ) {
            // Loop through product category terms  set for the current product
            foreach( $terms as $term) {
                foreach( get_ancestors( $term, $taxonomy ) as $term_id ) {
                    $ancestors[(int) $term_id] = (int) $term_id;
                }
            }

            // Loop through cart items
            foreach ( $cart->get_cart() as $item ) {
                $terms = (array) wp_get_post_terms( $item['product_id'], $taxonomy, array('fields' => 'ids') );
                if( count($terms) > 0 ) {
                    // Loop through product category terms set for the current cart item
                    foreach( $terms as $term) {
                        foreach( get_ancestors( $term, $taxonomy ) as $term_id ) {
                            $ancestors[(int) $term_id] = (int) $term_id;
                        }
                    }
                }
            }

            // When there is more than 1 parent product category
            if( count($ancestors) > 1 ) {
                wc_add_notice( __('Disculpa, NO puedes agregar a la misma orden/pedido productos que pertenezcan a distintas preventas o proveedores. Por favor, escoge solo productos que pertenezcan una misma preventa o proveedor.'), 'error' );
                $passed = false; 
            }
        }
        return $passed;
    }
}
add_filter( 'woocommerce_add_to_cart_validation', 'avoid_add_to_cart_from_different_main_categories', 10, 3 );

我會很感激你的幫助。

您需要在 function 中使用用戶角色 IF 語句條件,如下所示:

function avoid_add_to_cart_from_different_main_categories( $passed, $product_id, $quantity ) {
    if( WC()->cart->is_empty() )
        return $passed;
        
    $user = wp_get_current_user();

    if ( in_array( 'franquicia', $user->roles ) ) {
        $taxonomy  = 'product_cat';
        $ancestors = [];

        $terms = (array) wp_get_post_terms( $product_id, $taxonomy, array('fields' => 'ids') );

        if( count($terms) > 0 ) {
            // Loop through product category terms  set for the current product
            foreach( $terms as $term) {
                foreach( get_ancestors( $term, $taxonomy ) as $term_id ) {
                    $ancestors[(int) $term_id] = (int) $term_id;
                }
            }

            // Loop through cart items
            foreach ( WC()->cart->get_cart() as $item ) {
                $terms = (array) wp_get_post_terms( $item['product_id'], $taxonomy, array('fields' => 'ids') );
                if( count($terms) > 0 ) {
                    // Loop through product category terms set for the current cart item
                    foreach( $terms as $term) {
                        foreach( get_ancestors( $term, $taxonomy ) as $term_id ) {
                            $ancestors[(int) $term_id] = (int) $term_id;
                        }
                    }
                }
            }

            // When there is more than 1 parent product category
            if( count($ancestors) > 1 ) {
                wc_add_notice( __('Disculpa, NO puedes agregar a la misma orden/pedido productos que pertenezcan a distintas preventas o proveedores. Por favor, escoge solo productos que pertenezcan una misma preventa o proveedor.'), 'error' );
                $passed = false; 
            }
        }
    }
    return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'avoid_add_to_cart_from_different_main_categories', 10, 3 );

代碼進入活動子主題(或活動主題)的functions.php文件。 它應該更好地工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM