Products dropdown from same category inside Woocommerce product short description

前端 未结 2 816
醉酒成梦
醉酒成梦 2021-01-23 17:37

In Woocommerce, I would like to add a drop down in product short description that shows all products that have the same category(ies). It will be even better if it was possible

2条回答
  •  北海茫月
    2021-01-23 18:04

    Add this to your theme's 'functions.php' which will display all products of your current product's category.

    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 '';
            endif;
        }
    }
    add_action( 'woocommerce_single_product_summary', 'add_products_short_description', 15 );
    

提交回复
热议问题