Skip to content

Instantly share code, notes, and snippets.

@mikejolley
Forked from corsonr/gist:6681929
Last active April 18, 2023 22:49
Show Gist options
  • Save mikejolley/9f5adb8d194d7681e7b7 to your computer and use it in GitHub Desktop.
Save mikejolley/9f5adb8d194d7681e7b7 to your computer and use it in GitHub Desktop.

Revisions

  1. mikejolley revised this gist Aug 15, 2015. 1 changed file with 21 additions and 23 deletions.
    44 changes: 21 additions & 23 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -7,41 +7,39 @@
    * 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 ) );

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

    // 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[ */
    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;
    /* <![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.onchange = onProductCatChange;
    product_cat_dropdown.change( onProductCatChange );
    });
    /* ]]> */
    </script>
    <?php

    return ob_get_clean();

    }
  2. @corsonr corsonr revised this gist Sep 24, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -14,8 +14,8 @@
    function woo_product_categories_dropdown( $atts ) {

    extract(shortcode_atts(array(
    'count' => '0',
    'hierarchical' => '0',
    'count' => '0',
    'hierarchical' => '0',
    'orderby' => ''
    ), $atts));

  3. @corsonr corsonr created this gist Sep 24, 2013.
    47 changes: 47 additions & 0 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    <?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();

    }