0
votes

I have to make a Wordpress plugin which adds shortcode for WooCommerce. I want to get products from a specific product category and the maximum number of products to show. The shortcode parameters should be category ID and product limit. I think I should have to use WP_Query object.

I need to get it look like this:

enter image description here

Shortcode would be like this: [productslist_category="[category_ID]" limit="[product_limit]"]

I used the code below from this answer (thanks to LoicTheAztec):

if( !function_exists('products_list_in_a_product_category') ) {

function products_list_in_a_product_category( $atts ) {

    // Shortcode Attributes
    $atts = shortcode_atts(
        array(
            'cat'       => '',
            'limit'     => '4', // default product per page
            'column'    => '4', // default columns
        ),
        $atts, 'productslist'
    );

    // The query
    $posts = get_posts( array(
        'post_type'      => 'product',
        'posts_per_page' => intval($atts['limit'])+1,
        'product_cat'    => $atts['cat'],
    ) );

    $output = '<div class="products-in-'.$atts['cat'].'">';

    // The loop
    foreach($posts as $post_obj)
        $ids_array[] = $post_obj->ID;

    $ids = implode( ',', $ids_array );

    $columns = $atts['column'];

    $output .= do_shortcode ( "[products ids=$ids columns=$columns ]" ) . '</div>';

    return $output;
}
add_shortcode( 'productslist', 'products_list_in_a_product_category' );}

But I get an error. It says that there is something wrong with the implode function.

2
i said that there is error in implode functionuser8198956
Warning: implode(): Invalid arguments passeduser8198956
No warning and no errors… see my 2 tested live links in my answer below. The error is due to something else in your installation. You need to use PHP 5.6 with WP and WooCommerce… This code perfectly worksLoicTheAztec

2 Answers

2
votes

Here is my original answer that was on your previous question you deleted, and that you where using here: Display WooCommerce products with a custom shortcode based on a category

The code works perfectly in woocommerce versions 2.6.x and 3+.


That was my original answer code that you have taken (before deleting your previous question):

Here is a solution based on your shortcode mixed with existing [product] WooCommerce shortcode. As you will see you will get what you are expecting…

Here is that code:

if( !function_exists('products_list_in_a_product_category') ) {

    function products_list_in_a_product_category( $atts ) {

        // Shortcode Attributes
        $atts = shortcode_atts(
            array(
                'cat'       => '',
                'limit'     => '5', // default product per page
                'column'    => '4', // default columns
            ),
            $atts, 'productslist'
        );

        // The query
        $posts = get_posts( array(
            'post_type'      => 'product',
            'posts_per_page' => intval($atts['limit'])+1,
            'product_cat'    => $atts['cat'],
        ) );

        $output = '<div class="products-in-'.$atts['cat'].'">';

        // The loop
        foreach($posts as $post_obj)
            $ids_array[] = $post_obj->ID;

        $ids = implode( ',', $ids_array );

        $columns = $atts['column'];

        $output .= do_shortcode ( "[products ids=$ids columns=$columns ]" ) . '</div>';

        return $output;
    }
    add_shortcode( 'productslist', 'products_list_in_a_product_category' );
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested on WooCommerce 3+ and works.


USAGE (Example):

[productslist cat="clothing" limit="4"]

you will get this:

enter image description here

-

-1
votes

$args = array( 'post_type' => 'product', 'post_status' => 'publish', 'ignore_sticky_posts' => 1, 'posts_per_page' => '12', 'meta_query' => array( array( 'key' => '_visibility', 'value' => array('catalog', 'visible'), 'compare' => 'IN' ) ), 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'term_id', //This is optional, as it defaults to 'term_id' 'terms' => 26, 'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'. ) ) );$products = new WP_Query($args);var_dump($products);