2016-12-07 2 views
1

Ich versuche, einen WooCommerce-Kategorien-Dropdown-Shortcode hinzuzufügen, aber das scheint nicht zu funktionieren. Ich kann das Dropdown sehen, aber wenn ich eine Kategorie auswähle, wird sie nicht registriert und nichts passiert.WooCommerce benutzerdefinierte Kategorien Dropdown-Shortcode-Ausgabe

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

Code, die

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

} 

Antwort

2

in meiner functions.php-Datei gespeichert ist, wenn Sie das lesen Kommentare für die Quelle dieses Codes, den Sie ausgewählt haben here, sie waren einige Fehler und sie fanden einige Wende vor kurzem.

Also die richtige aktualisierte Code scheint dies zu sein:

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

Aber es wird nicht funktionieren, da woocommerce_product_dropdown_categories() ist ein Buggy-Funktion, die veraltet ist. Siehe this reference über.

Möglicherweise können Sie versuchen, stattdessen this plugin zu diesem Zweck zu verwenden.

0

Für alle noch auf diese stecken, dann ist hier die zur Zeit Arbeitslösung:

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