Display with a shortcode a dropdown of Woocommerce products for a specific category

Luqmanul Hakim

i want to display product by category with dropdown

and i made this code but why the value not going inside the ?

function rakitpc() {
    $args = array(
    'posts_per_page' => -1,
    'product_cat' => 'Tshirts',
    'hide_empty' => 0,
    'post_status' => 'publish',
    'post_type' => 'product',
    'orderby' => 'title',
    'echo' => '0'
    );
    $products = new WP_Query( $args );
    $output = '<select>';
    // foreach ( $products as $product ) {
    while ( $products->have_posts() ) {
        $products->the_post();
        $aa = the_permalink($post);
        echo $aa;
        $bb = the_title($post);
        $output .= "<option value=\"<?php the_permalink(); ?>\"> <?php the_permalink(); ?></option>";  
    }
    wp_reset_query();
    $output .='</select>';
    return $output;
    }
add_shortcode( 'gege', 'rakitpc' );

This is the output That I get:

output

My reference: How do I display a "category products drop down in a wordpress page" in Woocommerce 2.5.2

LoicTheAztec

Try the following that will give you a dropdown with the permalinks as <option> values displaying the product names (see the screenshot at the end).

You will need to set your category name (or categories names) in the array inside the funtion.

function rakitpc() {
    // HERE below, define your Product category names
    $term_names = array('T-shirts');

    // The WP_Query
    $query = new WP_Query( array(
        'posts_per_page' => -1,
        'post_type' => 'product',
        'post_status' => 'publish',
        'hide_empty' => 0,
        'orderby' => 'title',
        'tax_query' => array( array(
            'taxonomy' => 'product_cat', // for a Product category (or 'product_tag' for a Product tag)
            'field'    => 'name',        // can be 'name', 'slug' or 'term_id'
            'terms'    => $term_names,
        ) ),
        'echo' => '0'
    ) );

    $output = '<select>';
    // foreach ( $products as $product ) {
    if ( $query->have_posts() ) :
    while ( $query->have_posts() ) : $query->the_post();

        $permalink = get_permalink($query->post->ID);
        $title = $query->post->post_title;
        $output .= '<option value="' . $permalink . '">' . $title . '</option>';

    endwhile;

    wp_reset_postdata();

    $output .='</select>';

    else :

    $output = '<p>No products found<p>';

    endif;

    return $output;
}

add_shortcode( 'gege', 'rakitpc' );

Code goes in function.php file of your active child theme (or active theme). Tested and works.

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Get all WooCommerce products from a category that have specific meta data

Woocommerce MySQL to update price of products in specific category

Display only "in stock" products in Woocommerce Recent Products shortcode

Display products from specific product category in Woocommerce

Hide products having a specific product category in Woocommerce

Nested shortcode to dynamically display WooCommerce products category

Dynamic sale price display for specific products in Woocommerce

Display WooCommerce products with a shortcode using a custom meta_query

Specifically ordering products in a woocommerce shortcode

Replace Upsells with products from a specific product category in Woocommerce 3

Modify Woocommerce template files to display products by product category

Add dropdown to products and display the value on Woocommerce cart

Woocommerce Products Offset Shortcode

Display All Products by Category with WooCommerce

Display a shorcode below products in Woocommerce Storefront Category archives?

Hide out of stock products on specific category archives In Woocommerce

Display a message if there are no products returned for a WooCommerce shortcode

Display related products using a shortcode on WooCommerce My account Dashboard

shortcode to show products by category woocommerce

Hide attribute dropdown for a specific WooCommerce product category

Output a custom shortcode for a specific product category in WooCommerce

Products dropdown from same category inside Woocommerce product short description

Woocommerce how to display products from specific category on archive page

Custom Woocommerce products list shortcode

woocommerce shortcode for products in brand and category

Exclude Products Category from loops shortcode Woocommerce

Apply discount on products within specific category (WooCommerce)

How can I display the category name with a shortcode with wordpress and woocommerce?

How To Filter and Display ONLY Simple WooCommerce Products using Shortcode