• Resolved danishnoorani

    (@danishnoorani)


    I need urgent assistance regarding displaying products by vendor id. To explain it a bit further. I need a function to be placed under a shortcode that would display all the published products posted by logged in vendors with my wp site using WCMP.

    I came across this class file but I would really appreciate it if you provide some assistance in making a custom function with shortcode please.

    
    /**
         * Get all products belonging to vendor
         * @param  $args (default=array())
         * @return arr Array of product post objects
         */
        public function get_products($args = array()) {
            global $WCMp;
            $default = array(
                'post_type' => 'product',
                'post_status' => 'publish',
                'posts_per_page' => -1,
                'author' => $this->id,
                'tax_query' => array(
                    array(
                        'taxonomy' => $WCMp->taxonomy->taxonomy_name,
                        'field' => 'term_id',
                        'terms' => absint($this->term_id)
                    )
                )
            );
    
            $args = wp_parse_args($args, $default);
            return get_posts( apply_filters( 'wcmp_get_vendor_products_query_args', $args, $this->term_id ) );
        }
    
Viewing 1 replies (of 1 total)
  • Hi,
    This code will help you to get login vendor all publish product..

    Add this code on your current active theme function.php

    function shortcode_get_products($args = array()) {
            global $WCMp;
            $user_id = get_current_user_id();
            if(is_user_wcmp_vendor($user_id)){
                $default = array(
                    'post_type' => 'product',
                    'post_status' => 'publish',
                    'posts_per_page' => -1,
                    'author' => $user_id, //change your vendor id here
                );
    
                $args = wp_parse_args($args, $default);
    
                ob_start();
    
                $products = new WP_Query( $args );
    
                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{
                    echo "No Product Found";
                }
    
                woocommerce_reset_loop();
                wp_reset_postdata();
    
                $return = '<div class="woocommerce ">' . ob_get_clean() . '</div>';
    
                // Remove ordering query arguments
                WC()->query->remove_ordering_args();
    
                return $return;
            }else{
                return $msg="<h2>You are not vendor</h2>";
            }
        }
    
        add_shortcode( 'shortcode_get_products', 'shortcode_get_products' );
Viewing 1 replies (of 1 total)
  • The topic ‘Show products by vendor’ is closed to new replies.