vapvarun icon

Display Services Products using Shortcode

vapvarun | PRO | 10/28/22 08:18:35 PM UTC | 0 ⭐ | 1548 👁️ | Never ⏰ | [woocommerce, Woo Sell Services]
PHP |

1.64 KB

|

Software

|

0 👍

/

0 👎

if( ! function_exists('product_service') ) {
 
    // Add Shortcode
    function product_service( $atts ) {
        global $woocommerce_loop;
 
        // Attributes 
        $atts = shortcode_atts(
            array(
                'columns'   => '4',
                'limit'     => '20',
            ),
            $atts, 'products_service'
        );
 
 
        $woocommerce_loop['columns'] = $atts['columns'];
        
        // The WP_Query
        $products = new WP_Query( array (
            'post_type'         => 'product',
            'post_status'       => 'publish',
            'posts_per_page'    => $atts['limit'],
              'meta_key' => '_wss_type',
            'meta_value' => array('yes'), //'meta_value' => array('yes'),
            'meta_compare' => 'IN' //'meta_compare' => 'NOT IN'
            
        ));
 
        ob_start();
 
        if ( $products->have_posts() ) { ?>
 
            <?php woocommerce_product_loop_start(); ?>
 
                <?php while ( $products->have_posts() ) : $products->the_post(); ?>
 
                    <?php wc_get_template_part( 'content', 'product' ); ?>
 
                <?php endwhile; // end of the loop. ?>
 
            <?php woocommerce_product_loop_end(); ?>
 
            <?php
        } else {
            do_action( "woocommerce_shortcode_products_loop_no_results", $atts );
            echo "<p>There is no results.</p>";
        }
 
        woocommerce_reset_loop();
        wp_reset_postdata();
 
        return '<div class="woocommerce columns-' . $atts['columns'] . '">' . ob_get_clean() . '</div>';
    }
 
    add_shortcode( 'products_service', 'product_service' );
}

Comments