Woocommerce自定义产品类别下拉问题 [英] Woocommerce custom product category dropdown issue
问题描述
我正在为店面主题开发一个子主题。我将产品类别窗口小部件用作标题下的一个下拉列表,它非常适合我的需求,尽管我需要相同的(如果可能)下拉菜单显示在每个类别页面上,而不仅仅是主页。
I am developing a child theme for the Storefront Theme. I use the Product Category Widget as a dropdown under the header which fits my needs perfectly, though I need the same (if possible) dropdown menu to show up on every Category Page, instead of just the Main Page.
我正在自定义此代码几乎可以做到这一点:
I am customizing this code which almost does it:
/**
* 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();
}
现在我需要隐藏计数器并显示空的类别。
Now I need to hide the counters and show the empty Categories.
我无法获取。
如何隐藏计数器并显示空的类别?
How can I hide the counters and show the empty Categories?
推荐答案
在您的代码中有:
- 代码中的一些错误,例如错误的'show_counts',即
'show_count'
(不包含s
)…现在,隐藏计数器已启用并起作用。 - 缺少参数'hide_empty'来显示空类别
- some mistakes in the code like wrong 'show_counts' that is
'show_count'
(withouts
) … Now hiding counters is enabled and functional. - missing argument 'hide_empty' to show empty categories
在此短代码中,您可以更改以下可选参数:
In this shortcode you can alter the following optional arguments:
-
分层
,默认情况下已禁用(设置为'0') -
hide_empty
默认情况下被禁用(设置为'0') -
show_count
默认情况下现在已禁用(设置为 0) -
深度
默认情况下被禁用(设置为'0') -
orderby
默认设置为类别 order(也可以使用名称: name)
hierarchical
that is disabled by default (set to '0')hide_empty
that is disabled by default (set to '0')show_count
that is now disabled by default (set to '0')depth
that is disabled by default (set to '0')orderby
set to category "order" by default (can be by names too: "name")
添加了自定义钩子 woocommerce_product_categories_shortcode_dropdown_args
将允许扩展自定义…
Added a custom hook woocommerce_product_categories_shortcode_dropdown_args
that will allow extended customizations…
这里是新代码:
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)用法示例-分层显示的所有产品类别和子类别:
1) Example usage - All product categories and subcategories hierarchically displayed:
[product_categories_dropdown orderby='name' hierarchical='1']
在php代码中,您可以通过以下方式使用它:
In php code you can use it this way:
echo do_shortcode("[product_categories_dropdown orderby='name' hierarchical='1']");
或插入html标签:
<?php echo do_shortcode("[product_categories_dropdown orderby='name' hierarchical='1']"); ?>
2)示例用法-仅主要父级产品类别:
2) Example usage - Only "main parent" product categories:
[product_categories_dropdown depth='1' hierarchical='1']
在php代码中,您可以通过以下方式使用它:
In php code you can use it this way:
echo do_shortcode("[product_categories_dropdown depth='1' hierarchical='1']");
或插入html标签:
<?php echo do_shortcode("[product_categories_dropdown depth='1' hierarchical='1']"); ?>
这篇关于Woocommerce自定义产品类别下拉问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!