从品牌和类别自定义下拉列表中过滤 WooCommerce 产品

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

我正在尝试创建 2 个下拉菜单,一个用于 WooCommerce 插件的 Perfect Brands 品牌,另一个用于二级产品类别。该类别本身有效,但品牌只是返回包含所有产品的商店页面。我查找了 WooCommerce 插件的 Perfect Brands 的分类法,我相信它是

pwb-brand
,这是唯一一个在下拉菜单中实际列出品牌的分类法。如果两个输入都被明确选择,我需要下拉菜单来排除产品,但到目前为止只有类别有效。

这是我的代码:

// Dropdown Function
function woocommerce_category_dropdown() {
    // Get all product categories excluding "ALL" and second-level categories
    $args = array(
        'taxonomy' => 'product_cat',
        'orderby' => 'name',
        'order'   => 'ASC',
        'hide_empty' => false,
    );
    $categories = get_terms($args);

    // Get all brands using Perfect Brands WooCommerce plugin
    $brands = get_terms(array(
        'taxonomy' => 'pwb-brand',
        'orderby' => 'name',
        'order'   => 'ASC',
        'hide_empty' => false,
    ));

    // Start output buffering
    ob_start();
    ?>

    <form role="search" method="get" id="categorysearchform" action="<?php echo esc_url(home_url('/')); ?>">
        <!-- Brand dropdown -->
        <select name="brand" id="brand">
            <option value=""><?php _e('Elige una marca', 'woocommerce'); ?></option>
            <?php foreach ($brands as $brand) : ?>
                <option value="<?php echo esc_attr($brand->slug); ?>"><?php echo esc_html($brand->name); ?></option>
            <?php endforeach; ?>
        </select>

        <!-- Second-level category dropdown excluding "ALL" -->
        <select name="product_cat" id="product_cat">
            <option value=""><?php _e('Elige una categoría', 'woocommerce'); ?></option>
            <?php foreach ($categories as $category) : ?>
                <?php
                // Skip the "ALL" category
                if ($category->name === 'ALL') {
                    continue;
                }

                // Get second-level categories
                if ($category->parent != 0) {
                    $parent = get_term($category->parent, 'product_cat');
                    if ($parent->parent == 0) { // Only show second-level categories
                        $parent_category = $category->name; // Remove arrow symbol
                        ?>
                        <option value="<?php echo esc_attr($category->slug); ?>"><?php echo esc_html($parent_category); ?></option>
                        <?php
                    }
                }
                ?>
            <?php endforeach; ?>
        </select>

        <input type="hidden" value="product" name="post_type" />
        <button type="submit" id="searchsubmit"><?php _e('Buscar', 'woocommerce'); ?></button>
    </form>

    <?php
    // Return the output
    return ob_get_clean();
}

add_shortcode('woocommerce_category_dropdown', 'woocommerce_category_dropdown');
php woocommerce product shortcode taxonomy-terms
1个回答
0
投票

代码中的主要问题是您需要在相关

<select>
字段上添加品牌分类“pwb-brand”作为属性名称。

我重新审视了你的代码:

add_shortcode('brand_category_filter', 'filter_products_from_brand_and_category_dropdowns');
function filter_products_from_brand_and_category_dropdowns() {
    // Get all product categories excluding "ALL" and second-level categories
    $categories = get_terms( array(
        'taxonomy' => 'product_cat',
        'orderby' => 'name',
        'order'   => 'ASC',
        'hide_empty' => false,
    ) );

    // Get all brands using Perfect Brands WooCommerce plugin
    $brands = get_terms( array(
        'taxonomy' => 'pwb-brand',
        'orderby' => 'name',
        'order'   => 'ASC',
        'hide_empty' => false,
    ) );

    ob_start(); // Start output buffering
    ?>
    <form role="search" method="get" id="categorysearchform" action="<?php echo esc_url(home_url('/')); ?>">
        <!-- Brand dropdown -->
        <select name="pwb-brand" id="pwb-brand">
            <option value=""><?php esc_html_e('Choose a brand', 'woocommerce'); ?></option>
            <?php // Loop through brands
            foreach ( $brands as $brand ) { 
                printf('<option value="%s">%s</option>', esc_attr($brand->slug), esc_html($brand->name) );
            } ?>
        </select>

        <!-- Second-level category dropdown excluding "ALL" -->
        <select name="product_cat" id="product_cat">
            <option value=""><?php _e('Choose a category', 'woocommerce'); ?></option>
            <?php // Loop through categories
            foreach ( $categories as $category ) {
                // Skip the "ALL" category
                if ($category->name === 'ALL') {
                    continue;
                }
                // Get second-level categories
                if ($category->parent != 0) {
                    $parent = get_term($category->parent, 'product_cat');
                    if ($parent->parent == 0) { // Only show second-level categories
                        printf('<option value="%s">%s</option>', esc_attr($category->slug), esc_html($category->name) );
                    }
                }
            } ?>
        </select>
        <input type="hidden" value="product" name="post_type" />
        <button type="submit" id="searchsubmit"><?php _e('Search', 'woocommerce'); ?></button>
    </form>
    <?php
    // Return the output
    return ob_get_clean();
}

简码用法:

[brand_category_filter]

代码位于子主题的functions.php 文件中(或插件中)。效果应该会更好。

© www.soinside.com 2019 - 2024. All rights reserved.