在 WooCommerce 中按品牌过滤

问题描述 投票:0回答:1

我正在尝试创建按品牌过滤的 woocommerce 过滤器插件,但它无法正常工作。我的产品设置正确,但它没有获取正确的结果。就像我有一个品牌 1 的记录,但它是返回品牌 2 的记录。

设置

    <?php
/*
Plugin Name: My WooCommerce Filter Plugin
Description: A simple product filter plugin for WooCommerce, including filters for price, brand, and color.
Version: 1.0
Author: Your Name
*/

if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly.
}

/**
 * Register Custom Taxonomies for Brand and Color
 */
add_action('init', 'register_custom_taxonomies_for_products');
function register_custom_taxonomies_for_products() 
{
    // Brand taxonomy
    $labels = array(
        'name'                       => _x('Brands', 'Taxonomy General Name', 'text_domain'),
        'singular_name'              => _x('Brand', 'Taxonomy Singular Name', 'text_domain'),
        // Further labels omitted for brevity
    );
    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => true,
        'public'                     => true,
        'show_ui'                    => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
    );
    register_taxonomy('brand', array('product'), $args);

    // Color taxonomy
    $labels = array(
        'name'                       => _x('Colors', 'Taxonomy General Name', 'text_domain'),
        'singular_name'              => _x('Color', 'Taxonomy Singular Name', 'text_domain'),
        // Further labels omitted for brevity
    );
    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => false,
        'public'                     => true,
        'show_ui'                    => true,
    );
    register_taxonomy('color', array('product'), $args);
}

/**
 * Display the filter form.
 */
add_action('woocommerce_before_shop_loop', 'my_wc_filter_form', 10);
function my_wc_filter_form() {
    $brands = get_terms(['taxonomy' => 'brand', 'hide_empty' => true]);

    $colors = get_terms(['taxonomy' => 'color', 'hide_empty' => true]);
    ?>
    <form role="filter" method="get" class="woocommerce-product-filter" action="<?php echo esc_url(home_url('/shop/')); ?>">
        <!-- Price Filter -->
        <input type="number" name="min_price" placeholder="Min price" style="width: 100px;">
        <input type="number" name="max_price" placeholder="Max price" style="width: 100px;">

        <!-- Brand Filter -->
        <select name="filter_brand">
            <option value="">Select a Brand</option>
            <?php foreach ($brands as $brand): ?>
                <option value="<?php echo esc_attr($brand->slug); ?>"><?php echo esc_html($brand->name); ?></option>
            <?php endforeach; ?>
        </select>

        <!-- Color Filter -->
        <select name="filter_color">
            <option value="">Select a Color</option>
            <?php foreach ($colors as $color): ?>
                <option value="<?php echo esc_attr($color->slug); ?>"><?php echo esc_html($color->name); ?></option>
            <?php endforeach; ?>
        </select>

        <button type="submit">Apply Filters</button>
    </form>
    <?php
}

/**
 * Adjust the product query based on filters.
 */
add_action( 'woocommerce_product_query', 'custom_product_query' );
function custom_product_query( $query ) {
    // Check if we are on the shop page or product archive page
    if ( ! is_admin() && is_shop() || is_product_category() || is_product_taxonomy() ) {
        // Get the values from the submitted form (use the correct form names or query vars)
     
        $brand = isset($_GET['brand']) ? sanitize_text_field($_GET['brand']) : '';
      

        // Meta query for filtering by price
       
        // Tax query for filtering by brand
        if ( $brand ) {
            $tax_query = $query->get('tax_query');
            if ( ! is_array( $tax_query ) ) {
                $tax_query = [];
            }

            $tax_query[] = [
                'taxonomy' => 'pa_brand',
                'field' => 'slug',
                'terms' => $brand
            ];

            $query->set('tax_query', $tax_query);
        }

      
    }
}
wordpress woocommerce hook-woocommerce
1个回答
0
投票

经过一番研究,这是有效的

add_action( 'woocommerce_product_query', 'custom_product_query' );
function custom_product_query( $query ) {
    // Check if we are on the shop page or product archive page
    if ( ! is_admin() && is_shop() || is_product_category() || is_product_taxonomy() ) {
        // Get the values from the submitted form (use the correct form names or query vars)
        $brand = isset($_GET['filter_brand']) ? sanitize_text_field($_GET['filter_brand']) : '';

        if ( $brand ) {
            $tax_query = $query->get('tax_query');
            if ( ! is_array( $tax_query ) ) {
                $tax_query = [];
            }
        
            $tax_query[] = [
                'taxonomy' => 'brand', // Here is where you use the correct taxonomy name
                'field' => 'slug',
                'terms' => $brand
            ];
        
            $query->set('tax_query', $tax_query);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.