【问题标题】:WooCommerce custom shortcode product categories dropdownWooCommerce 自定义简码产品类别下拉列表
【发布时间】:2016-12-07 23:11:19
【问题描述】:

我正在尝试添加 WooCommerce 类别下拉短代码,但这似乎不起作用。我可以看到下拉列表,但是当我选择一个类别时,它没有注册,也没有任何反应。

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

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

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

 }

【问题讨论】:

    标签: javascript php jquery woocommerce shortcode


    【解决方案1】:

    更新(2019)

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

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

    新的工作代码:

    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();
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

    用法示例:

    1) 在页面或帖子内容文本编辑器内(或文本小部件)

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

    2) 在 php 模板或代码上:

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

    原答案:

    如果您阅读了您选择here 的代码源的 cmets,它们是一些错误,并且他们最近发现了一些变化。

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

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

    但它不起作用,因为 woocommerce_product_dropdown_categories() 是一个已弃用的错误函数。见this referenceabout。

    也许您可以尝试使用 this plugin 来代替。

    【讨论】:

      【解决方案2】:

      对于仍然坚持这一点的任何人,这是当前有效的解决方案:

      /**
       * 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
          wc_product_dropdown_categories( $c, $h, 0, $o );
          ?>
          <script type='text/javascript'>
          /* <![CDATA[ */
              var product_cat_dropdown = jQuery(".dropdown_product_cat");
              product_cat_dropdown.change(function() {
                  if ( product_cat_dropdown.val() !=='' ) {
                      location.href = "<?php echo home_url(); ?>/?product_cat="+product_cat_dropdown.val();
                  }
              });
          /* ]]> */
          </script>
          <?php
      
          return ob_get_clean();
      }
      

      【讨论】:

      • 知道我如何修改它以显示一个类别的孩子而不是所有类别的列表吗?
      猜你喜欢
      • 2018-10-03
      • 1970-01-01
      • 2019-01-20
      • 1970-01-01
      • 2020-02-06
      • 2018-01-25
      • 2017-12-29
      • 1970-01-01
      相关资源
      最近更新 更多