WooCommerce 自定义简码产品类别下拉菜单 [英] WooCommerce custom shortcode product categories dropdown

查看:36
本文介绍了WooCommerce 自定义简码产品类别下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试添加 WooCommerce 类别下拉列表简码,但这似乎不起作用.我能够看到下拉菜单,但是当我选择一个类别时,它不会注册,也没有任何反应.

sc:[product_categories_dropdown orderby="title" count="0" hierarchy="0"]

放在我的functions.php文件中的代码

'0','分层' =>'0','orderby' =>''), $atts));ob_start();$c = $count;$h = $hierarchical;$o = ( isset( $orderby ) && $orderby != '' ) ?$orderby : '订单';//坚持这个直到修复 http://core.trac.wordpress.org/ticket/13258woocommerce_product_dropdown_categories( $c, $h, 0, $o );?><script type='text/javascript'>/* <![CDATA[ */var product_cat_dropdown = document.getElementById("dropdown_product_cat");功能 onProductCatChange() {if ( product_cat_dropdown.options[product_cat_dropdown.selectedIndex].value !=='' ) {location.href = "<?php echo home_url(); ?>/?product_cat="+product_cat_dropdown.options[product_cat_dropdown.selectedIndex].value;}}product_cat_dropdown.onchange = onProductCatChange;/* ]]>*/<?php返回 ob_get_clean();}

解决方案

更新 (2019)

以下是自 woocommerce 3 发布以来此自定义产品类别下拉短代码的工作代码版本:

  • wc_product_dropdown_categories() 替换 woocommerce_product_dropdown_categories() 自 woocommerce 3 起已弃用
  • 修改了 Javascript/jQuery 代码.
  • 其他一些光线变化.

新的工作代码:

add_shortcode( 'product_categories_dropdown', 'shortcode_product_categories_dropdown' );函数shortcode_product_categories_dropdown( $atts ) {//简码属性$atts = shortcode_atts( 数组('show_count' =>'0','分层' =>'0','orderby' =>''), $atts, 'product_categories_dropdown' );ob_start();wc_product_dropdown_categories( 数组('show_count' =>$atts['show_count'],'分层' =>$atts['分层'],'orderby' =>( isset($atts['orderby']) && empty($atts['orderby']) ? $atts['orderby'] : 'order' ),) );?><script type='text/javascript'>jQuery(函数($){$('.dropdown_product_cat').change(function(){if( $(this).val() !=='' ) {location.href = '<?php echo home_url();?>/?product_cat='+$(this).val();}});});<?php返回 ob_get_clean();}

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

用法示例:

1) 在页面内或发布内容文本编辑器(或文本小部件):

[product_categories_dropdown orderby="title" count="0" hierarchy="0"]

2) 在 php 模板或代码上:

echo do_shortcode("[product_categories_dropdown orderby='title' count='0'hierarchy='0']");

<小时>

原答案:

如果您阅读了您选择的此代码来源的评论此处,他们是一些错误,他们最近发现了一些好转.

所以正确的更新代码似乎是这个:

/*** WooCommerce 额外功能简码* --------------------------** 注册一个创建产品类别下拉列表的短代码** 使用:[product_categories_dropdown orderby="title" count="0" hierarchy="0"]**/add_shortcode('product_categories_dropdown', 'woo_product_categories_dropdown');功能 woo_product_categories_dropdown( $atts ) {提取(shortcode_atts(数组('show_count' =>'0','分层' =>'0','orderby' =>''), $atts));ob_start();$c = $count;$h = $hierarchical;$o = ( isset( $orderby ) && $orderby != '' ) ?$orderby : '订单';//坚持这个直到修复 http://core.trac.wordpress.org/ticket/13258woocommerce_product_dropdown_categories( $c, $h, 0, $o );?><script type='text/javascript'>/* <![CDATA[ */var product_cat_dropdown = jQuery(".dropdown_product_cat");功能 onProductCatChange() {if ( product_cat_dropdown.options[product_cat_dropdown.selectedIndex].value !=='' ) {location.href = "<?php echo home_url(); ?>/?product_cat="+product_cat_dropdown.options[product_cat_dropdown.selectedIndex].value;}}product_cat_dropdown.onchange = onProductCatChange;/* ]]>*/<?php返回 ob_get_clean();}

<块引用>

但它不起作用,因为 woocommerce_product_dropdown_categories() 是一个已弃用的错误函数.请参阅此参考资料.>

也许你可以尝试使用这个插件 相反,为此目的.

I am trying to add a WooCommerce categories-dropdown-shortcode, but this seems to not work. I am able to see the drop down, but when i choose a category it doesn't register and nothing happens.

sc: [product_categories_dropdown orderby="title" count="0" hierarchical="0"]

Code that's placed in my functions.php file

<?php
/**
 * 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();

    $c = $count;
$h = $hierarchical;
$o = ( isset( $orderby ) && $orderby != '' ) ? $orderby : 'order';

// Stuck with this until a fix for      http://core.trac.wordpress.org/ticket/13258
woocommerce_product_dropdown_categories( $c, $h, 0, $o );
?>
<script type='text/javascript'>
/* <![CDATA[ */
    var product_cat_dropdown = document.getElementById("dropdown_product_cat");
    function onProductCatChange() {
        if ( product_cat_dropdown.options[product_cat_dropdown.selectedIndex].value !=='' ) {
            location.href = "<?php echo home_url(); ?>/?product_cat="+product_cat_dropdown.options[product_cat_dropdown.selectedIndex].value;
        }
    }
    product_cat_dropdown.onchange = onProductCatChange;
/* ]]> */
</script>
<?php

return ob_get_clean();

 }

解决方案

Update (2019)

Here below is the working code version for this custom product category dropdown shortcode since woocommerce 3 release:

  • wc_product_dropdown_categories() replace woocommerce_product_dropdown_categories() which was deprecated since woocommerce 3
  • Modified Javascript/jQuery code.
  • Some other light changes.

The new working code:

add_shortcode( 'product_categories_dropdown', 'shortcode_product_categories_dropdown' );
function shortcode_product_categories_dropdown( $atts ) {
    // Shortcode Attributes
    $atts = shortcode_atts( array(
        'show_count'    => '0',
        'hierarchical'  => '0',
        'orderby'       => ''
    ), $atts, 'product_categories_dropdown' );

    ob_start();

    wc_product_dropdown_categories( array(
        'show_count'    => $atts['show_count'],
        'hierarchical'  => $atts['hierarchical'],
        'orderby'       => ( isset($atts['orderby'])  && empty($atts['orderby']) ? $atts['orderby'] : 'order' ),
    ) );
    ?>
    <script type='text/javascript'>
    jQuery(function($) {
        $('.dropdown_product_cat').change(function(){
            if( $(this).val() !=='' ) {
                location.href = '<?php echo home_url(); ?>/?product_cat='+$(this).val();
            }
        });
    });
    </script>
    <?php

    return ob_get_clean();
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

USAGE example:

1) Inside a Page or post content text Editor (or a text widget):

[product_categories_dropdown orderby="title" count="0" hierarchical="0"]

2) On a php template or code:

echo do_shortcode("[product_categories_dropdown orderby='title' count='0' hierarchical='0']");


Original answer:

If you read the comments for the source of this code that you have picked here, they were some errors and they found some turn around recently.

So the correct updated code seems to be this one:

/**
 * WooCommerce Extra Feature Shortcode
 * --------------------------
 *
 * 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(
        'show_count'    => '0',
        'hierarchical'  => '0',
        'orderby'       => ''
    ), $atts));

    ob_start();

    $c = $count;
    $h = $hierarchical;
    $o = ( isset( $orderby ) && $orderby != '' ) ? $orderby : 'order';

    // Stuck with this until a fix for http://core.trac.wordpress.org/ticket/13258
    woocommerce_product_dropdown_categories( $c, $h, 0, $o );
    ?>
    <script type='text/javascript'>
    /* <![CDATA[ */
        var product_cat_dropdown = jQuery(".dropdown_product_cat");
        function onProductCatChange() {
            if ( product_cat_dropdown.options[product_cat_dropdown.selectedIndex].value !=='' ) {
                location.href = "<?php echo home_url(); ?>/?product_cat="+product_cat_dropdown.options[product_cat_dropdown.selectedIndex].value;
            }
        }
        product_cat_dropdown.onchange = onProductCatChange;
    /* ]]> */
    </script>
    <?php

    return ob_get_clean();
}

But it will not work, as woocommerce_product_dropdown_categories() is a buggy function that is deprecated. See this reference about.

May be you could try to use this plugin instead, for that purpose.

这篇关于WooCommerce 自定义简码产品类别下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆