Woocommerce 自定义产品类别下拉问题

Woocommerce custom product category dropdown issue

我正在为店面主题开发 child 主题。我使用产品类别小部件作为 header 下的下拉菜单,这完全符合我的需要,尽管我需要相同的(如果可能的话)下拉菜单显示在每个类别页面上,而不仅仅是主页。

我正在定制 this code 几乎可以做到:

/**
 * WooCommerce Extra Feature
 * --------------------------
 *
 * Register a shortcode that creates a product categories dropdown list
 *
 * Use: [product_categories_dropdown orderby="title" count="0" hierarchical="0"]
 */
add_shortcode( 'product_categories_dropdown', 'woo_product_categories_dropdown' );
function woo_product_categories_dropdown( $atts ) {
    extract( shortcode_atts(array(
        'count'        => '0',
        'hierarchical' => '0',
        'orderby'      => ''
    ), $atts ) );
    ob_start();
    // Stuck with this until a fix for http://core.trac.wordpress.org/ticket/13258
    wc_product_dropdown_categories( array(
        'orderby'            => ! empty( $orderby ) ? $orderby : 'order',
        'hierarchical'       => $hierarchical,
        'show_uncategorized' => 0,
        'show_counts'        => $count
    ) );
    ?>
    <script type='text/javascript'>
        /* <![CDATA[ */
        jQuery(function(){
            var product_cat_dropdown = jQuery(".dropdown_product_cat");
            function onProductCatChange() {
                if ( product_cat_dropdown.val() !=='' ) {
                    location.href = "<?php echo esc_url( home_url() ); ?>/?product_cat=" +product_cat_dropdown.val();
                }
            }
            product_cat_dropdown.change( onProductCatChange );
        });
    /* ]]> */
    </script>
    <?php
    return ob_get_clean();
}

现在我需要隐藏计数器并显示空类别。

我没听懂。

如何隐藏计数器并显示空类别?

在您的代码中有:

  • 代码中有些错误如错误的'show_counts''show_count'(没有s) …现在 隐藏计数器 已启用并起作用。
  • 缺少参数 'hide_empty' 显示空类别

在此短代码中,您可以更改以下可选参数:

  • hierarchical 默认情况下禁用(设置为“0”)
  • hide_empty 默认情况下禁用(设置为“0”)
  • show_count 现在默认禁用 (设置为“0”)
  • depth 默认情况下禁用(设置为“0”)
  • orderby 默认设置为类别 "order"(也可以按名称:"name")

添加了一个自定义挂钩 woocommerce_product_categories_shortcode_dropdown_args 允许扩展自定义…

这是新代码:

add_shortcode( 'product_categories_dropdown', 'woo_product_categories_dropdown' );
function woo_product_categories_dropdown( $atts ) {
    // Attributes
    $atts = shortcode_atts( array(
        'hierarchical' => '0', // or '1'
        'hide_empty'   => '0', // or '1'
        'show_count'   => '0', // or '1'
        'depth'        => '0', // or Any integer number to define depth
        'orderby'      => 'order', // or 'name'
    ), $atts, 'product_categories_dropdown' );

    ob_start();

    wc_product_dropdown_categories( apply_filters( 'woocommerce_product_categories_shortcode_dropdown_args', array(
        'depth'              => $atts['depth'],
        'hierarchical'       => $atts['hierarchical'],
        'hide_empty'         => $atts['hide_empty'],
        'orderby'            => $atts['orderby'],
        'show_uncategorized' => 0,
        'show_count'         => $atts['show_count'],
    ) ) );

    ?>
    <script type='text/javascript'>
        jQuery(function($){
            var product_cat_dropdown = $(".dropdown_product_cat");
            function onProductCatChange() {
                if ( product_cat_dropdown.val() !=='' ) {
                    location.href = "<?php echo esc_url( home_url() ); ?>/?product_cat=" +product_cat_dropdown.val();
                }
            }
            product_cat_dropdown.change( onProductCatChange );
        });
    </script>
    <?php

    return ob_get_clean();
}

代码进入活动子主题(或活动主题)的 function.php 文件。

经过测试并有效。


1) 用法示例 - 所有产品类别和子类别分层显示:

[product_categories_dropdown orderby='name' hierarchical='1']

在 php 代码中你可以这样使用它:

echo do_shortcode("[product_categories_dropdown orderby='name' hierarchical='1']");

或插入 html 标签:

<?php echo do_shortcode("[product_categories_dropdown orderby='name' hierarchical='1']"); ?>

2) 用法示例 - 仅 "main parent" 产品类别:

[product_categories_dropdown depth='1' hierarchical='1']

在 php 代码中你可以这样使用它:

echo do_shortcode("[product_categories_dropdown depth='1' hierarchical='1']");

或插入 html 标签:

<?php echo do_shortcode("[product_categories_dropdown depth='1' hierarchical='1']"); ?>

这是默认显示的方式,为此无需添加更多代码。 他们的方法是将产品分组到所属的产品类别中,并且每个类别的产品都有一个 drop-down,而不是一个。

示例:

汽车(下拉菜单)

  • 奥迪
  • 梅迪斯
  • 宝马

摩托车(下拉)

  • 本田
  • 雅马哈
  • 杜卡迪