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

loo*_*kos 2 wordpress categories shortcode woocommerce drop-down-menu

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

我正在自定义这段代码,几乎可以做到:

/**
 * 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();
}
Run Code Online (Sandbox Code Playgroud)

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

我无法得到那个。

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

Loi*_*tec 5

在您的代码中有:

  • 像错误代码中的一些错误“show_counts”这是'show_count' (没有s ......现在躲在柜台启用和功能。
  • 缺少参数“hide_empty”以显示空类别

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

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

添加了一个自定义钩子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();
}
Run Code Online (Sandbox Code Playgroud)

代码位于活动子主题(或活动主题)的 function.php 文件中。

测试和工作。


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

[product_categories_dropdown orderby='name' hierarchical='1']
Run Code Online (Sandbox Code Playgroud)

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

echo do_shortcode("[product_categories_dropdown orderby='name' hierarchical='1']");
Run Code Online (Sandbox Code Playgroud)

或插入到 html 标签中:

<?php echo do_shortcode("[product_categories_dropdown orderby='name' hierarchical='1']"); ?>
Run Code Online (Sandbox Code Playgroud)

2)示例用法- 仅“主要父级”产品类别:

[product_categories_dropdown depth='1' hierarchical='1']
Run Code Online (Sandbox Code Playgroud)

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

echo do_shortcode("[product_categories_dropdown depth='1' hierarchical='1']");
Run Code Online (Sandbox Code Playgroud)

或插入到 html 标签中:

<?php echo do_shortcode("[product_categories_dropdown depth='1' hierarchical='1']"); ?>
Run Code Online (Sandbox Code Playgroud)