1. Startseite
  2. Wissensdatenbank
  3. WooCommerce Großhandelspreise Premium
  4. Produktsichtbarkeit
  5. So blenden Sie Einzelhandelskategorien für Großhandelsbenutzer aus
  1. Startseite
  2. Wissensdatenbank
  3. WooCommerce Großhandelspreise Premium
  4. So blenden Sie Einzelhandelskategorien für Großhandelsbenutzer aus

So blenden Sie Einzelhandelskategorien für Großhandelsbenutzer aus

Wir haben eine Option, um Nur Großhandelsprodukte für Großhandelskunden anzeigen . Diese Funktion verbirgt jedoch nicht die Einzelhandelskategorien, die in Ihrem Shop angezeigt werden.

Glücklicherweise gibt es benutzerdefinierte Snippets, mit denen Sie unerwünschte Einzelhandelskategorien vor Ihren Großhandelskunden ausblenden können. Bitte fügen Sie den folgenden Snippet zu Ihrer child theme’s function.php hinzu.

Bitte beachten Sie, dass die Funktion Nur Großhandelsprodukte für Großhandelskunden anzeigen aktiviert sein muss, damit der Snippet funktioniert. Sie finden sie unter Woocommerce > Einstellungen > Großhandelspreise > Allgemein.

add_filter('get_terms', function ($terms, $tax, $qvars, $term_query) {
    if (is_shop() || is_product_category()) {
        global $wc_wholesale_prices_premium;

        if (!isset($wc_wholesale_prices_premium)) {
            return $terms;
        }

        $user_wholesale_role = $wc_wholesale_prices_premium->wwpp_wholesale_roles->getUserWholesaleRole();
        $wholesale_role = isset($user_wholesale_role[0]) ? $user_wholesale_role[0] : '';

        foreach ($terms as $key => $term) {
            //  Ensure $term is an object before accessing its properties
            if (!is_object($term) || !isset($term->term_id)) {
                continue; // Skip this term if invalid
            }

            $product_ids = array();
            $products = WWPP_WPDB_Helper::get_products_by_category($term->term_id) ?? array();

            foreach ($products as $product) {
                if (is_object($product) && isset($product->ID)) { // Added check for object validity
                    $product_ids[] = $product->ID;
                }
            }

            if (!empty($wholesale_role)) {
                $restricted_cat_ids = $wc_wholesale_prices_premium->wwpp_query->_get_restricted_product_cat_ids_for_wholesale_user($wholesale_role);
            } else {
                $restricted_cat_ids = get_option(WWPP_OPTION_PRODUCT_CAT_WHOLESALE_ROLE_FILTER, array());
            }

            $restricted_cat_ids = !empty($restricted_cat_ids) ? array_map('intval', $restricted_cat_ids) : array();

            $args = array(
                'post_type'      => 'product',
                'post_status'    => 'publish',
                'posts_per_page' => -1,
                'fields'         => 'ids',
                'post__in'       => $product_ids,
                'meta_query'     => array(
                    array(
                        'key'     => WWPP_PRODUCT_WHOLESALE_VISIBILITY_FILTER,
                        'value'   => array($wholesale_role, 'all'),
                        'compare' => 'IN'
                    )
                ),
                'tax_query'      => empty($restricted_cat_ids) ? array() : array(
                    array(
                        'taxonomy' => 'product_cat',
                        'field'    => 'term_id',
                        'terms'    => $restricted_cat_ids,
                        'operator' => 'NOT IN'
                    )
                )
            );

            if (!empty($user_wholesale_role) &&
                get_option('wwpp_settings_only_show_wholesale_products_to_wholesale_users') === 'yes' &&
                !WWPP_Helper_Functions::_wholesale_user_have_override_per_user_discount($user_wholesale_role) &&
                !WWPP_Helper_Functions::_wholesale_user_have_general_role_discount($wholesale_role)) {

                $args['meta_query'][] = array(
                    'relation' => 'OR',
                    array(
                        'key'     => $wholesale_role . '_have_wholesale_price',
                        'value'   => 'yes',
                        'compare' => '='
                    ),
                    array(
                        'key'     => $wholesale_role . '_wholesale_price',
                        'value'   => 0,
                        'compare' => '>',
                        'type'    => 'NUMERIC'
                    )
                );
            }

            $wholesale_query = new WP_Query($args);

            if (!$wholesale_query->have_posts()) {
                unset($terms[$key]);
            }
        }

        // Reset array keys after removing terms
        $terms = array_values($terms);
    }

    return $terms;
}, 20, 4);
War dieser Artikel hilfreich?

Verwandte Artikel

Benötigen Sie Unterstützung?

Können Sie die Antwort, die Sie suchen, nicht finden?
Support kontaktieren
Kauf abschließen