WooCommerce shortcode products list

我们两清 提交于 2019-12-24 20:14:41

问题


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.


回答1:


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:

-




回答2:


$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);



来源:https://stackoverflow.com/questions/45276742/woocommerce-shortcode-products-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!