简体   繁体   中英

WooCommerce shortcode products list

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:

在此处输入图片说明

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.

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:

在此处输入图片说明

-

$ 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',//这是可选的,因为它默认为'term_id''terms'=> 26,'operator'=>'IN'//可能的值为'IN','NOT IN','AND'。)))); $ products = new WP_Query($ args); var_dump($ products);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM