Thursday, 8 August 2013

wordpress woo-commerce code to get products of specific category

function getCatPro($atts)
{
extract( shortcode_atts( array(
'category' => '',
), $atts ) );
echo "<ul class='products'>";
        $args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => $category, 'orderby' => 'rand' );
        $loop = new WP_Query( $args );
        while ( $loop->have_posts() ) : $loop->the_post(); global $product;

                echo "<li class='product'>";  

                echo "<a href='".get_permalink( $loop->post->ID )."' title=".esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID)."'>";

                        woocommerce_show_product_sale_flash( $post, $product );
                        echo "<h3>".the_title()."</h3>";

                        echo "<span class='price'>".$product->get_price_html()."</span>";                    echo "</a>";

                  woocommerce_template_loop_add_to_cart( $loop->post, $product );

                echo "</li>";

    endwhile;
wp_reset_query();
echo "</ul>";
}

add_shortcode('proofcat','getCatPro');


//short code
[proofcat category='shoe']

You can use do_shortcode("[proofcat category='shoe']"); to call it in yor php file