Woocommerce定制产品类别下拉问题

时间:2018-01-31 17:00:26

标签: wordpress drop-down-menu woocommerce categories shortcode

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

我正在定制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();
}

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

我无法得到它。

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

2 个答案:

答案 0 :(得分:2)

在您的代码中有:

  • 代码中的一些错误,例如错误的'show_counts',即'show_count' (没有s ...现在隐藏计数器< / strong>已启用并正常运行。
  • 缺少参数'hide_empty'到显示空类别

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

  • hierarchical默认情况下已禁用(设为“0”)
  • hide_empty默认情况下已禁用(设为“0”)
  • show_count 现在默认禁用(设置为'0')
  • depth默认情况下已禁用(设为“0”)
  • orderby默认设置为“订单”类别(也可以是名称:“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)使用示例 - 仅“主要”产品类别:

[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']"); ?>

答案 1 :(得分:0)

这是默认情况下的显示方式,因此无需添加更多代码。 他们将产品归为所属产品类别,并且每个产品类别都有一个下拉列表,而不是一个下拉列表。

示例:

汽车(下拉列表)

  • 奥迪
  • Merdeces
  • 宝马

摩托车(下拉菜单)

  • 本田
  • 雅马哈
  • 杜卡迪