In Woocommerce möchte ich eine Dropdown-Liste in der Produktkurzbeschreibung hinzufügen, die alle Produkte anzeigt, die dieselbe(n) Kategorie(n) haben. Noch besser wäre es, wenn es möglich wäre, auf die Produktseite des ausgewählten Produkts zu gehen.
Ich habe keine Threads gesehen, die das erfüllen, was ich versuche zu tun.
Jede Hilfe wird geschätzt.
2021-Aktualisierung – Hinzugefügt product_id
als Argument, wodurch der Shortcode für eine definierte Produkt-ID (z. B. auf einer Seite) verwendet werden kann.
Im Folgenden wird ein benutzerdefinierter Shortcode erstellt, den Sie in Ihrer Produktkurzbeschreibung (oder sogar in der Produktbeschreibung) verwenden können, und es wird ein Dropdown-Menü aus derselben Produktkategorie wie das aktuelle Produkt angezeigt.
Der Code:
add_shortcode( 'products_dropdown', 'wc_products_from_cat_dropdown' );
function wc_products_from_cat_dropdown( $atts ) {
// Shortcode Attributes
$atts = shortcode_atts( array(
'product_id' => '',
), $atts, 'products_dropdown' );
$product_id = is_product() ? get_the_id() : $atts['product_id'];
if ( empty($product_id) )
return;
ob_start();
$query = new WP_Query( array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => '-1',
'post__not_in' => array( $product_id ),
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'ids' ) ) ,
) ),
) );
if ( $query->have_posts() ) :
echo '<div class="products-dropdown"><select name="products-select" id="products-select">
<option value="">'.__('Choose a related product').'</option>';
while ( $query->have_posts() ) : $query->the_post();
echo '<option value="'.get_permalink().'">'.get_the_title().'</option>';
endwhile;
echo '</select> <button type="button" style="padding:2px 10px; margin-left:10px;">'._("Go").'</button></div>';
wp_reset_postdata();
endif;
?>
<script type="text/javascript">
jQuery(function($){
var a=".products-dropdown", b = a+' button', c = a+' select', s="";
$(c).change(function(){
s = $(this).val();
console.log(s); // just for testing (to be removed)
});
$(b).click(function(){
if( s != '' ) location.href = s;
});
});
</script>
<?php
return ob_get_clean();
}
Der Code wird in die Datei functions.php Ihres aktiven untergeordneten Designs (oder aktiven Designs) eingefügt. Getestet und funktioniert.
VERWENDUNGSZWECK
1). Für einzelne Produktseiten: Fügen Sie einfach den folgenden Shortcode in die Produktkurzbeschreibung (oder -beschreibung) ein:
[products_dropdown]
2). Für einzelne Produktseiten im PHP-Code:
echo do_shortcode("[products_dropdown]");
3). Definieren Sie auf jedem Beitrag oder jeder Seite im Texteditor das Argument product_id (unterhalb der definierten Produkt-ID steht 37
):
[products_dropdown product_id="37"]


Fügen Sie dies der ‘functions.php’ Ihres Themas hinzu, die alle Produkte der Kategorie Ihres aktuellen Produkts anzeigt.
function add_products_short_description() {
$product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
if ( $product_cats ) {
$single_cat = array_shift( $product_cats );
$product_args = array( 'post_type' => 'product', 'posts_per_page' => '-1', 'product_cat' => $single_cat->name );
$products = new WP_Query( $product_args );
if ( $products->have_posts() ) : echo '<ul>';
while ( $products->have_posts() ) : $products->the_post(); global $product;
echo '<li><a href="'.get_permalink(%20$products->ID%20).'">'.get_the_title($products->ID).'</a></li>';
endwhile;
echo '</ul>';
endif;
}
}
add_action( 'woocommerce_single_product_summary', 'add_products_short_description', 15 );