WooCommerce comes with standard shortcode [product_category].
I wanted to get a shortcode with the same behavior, but filtering per vendor (a custom taxonomy) instead of per category.
So I have replicated and modified the code of [product_category], directly in woocommerce file class-wc-shortcodes.php to create [product_vendor].

This way, it is working fine. But I know it is not proper because my shortcodes will be erased at each upgrade.

So I tried to create my own plugin to host the code of [product_vendor].
But simply moving the code into it, is not working (my website gets down). I also tried to declare global $woocommerce at the begining. Not working either. I guess it is because the code is refering to elements of class WC_Shortcodes and woocommerce functions… but I am not at ease with class so I don’t know how to solve the issue…!

Here is the code I wrote in class-wc-shortcodes.php. Could you tell me what are the changes I should do so that this code is working in my own plugin ?

Many thanks

class WC_Shortcodes {

/**
 * Init shortcodes.
 */
public static function init() {
    $shortcodes = array(
        'product'                    => __CLASS__ . '::product',
        'product_page'               => __CLASS__ . '::product_page',
        'product_category'           => __CLASS__ . '::product_category',
        'product_vendor'             => __CLASS__ . '::product_vendor',  //new shortcode

...
 }

(…)

/**
 * Shortcode : List of products per vendor.
 */
public static function product_vendor( $atts ) {
    $atts = shortcode_atts( array(
        'per_page' => '12',
        'columns'  => '4',
        'orderby'  => 'category',
        'order'    => 'asc',
        'vendor' => '',  // Slugs
        'operator' => 'IN'
    ), $atts, 'product_vendor' );

    if ( ! $atts['vendor'] ) {
        return ''; 
    }

    // Default ordering args
    $ordering_args = WC()->query->get_catalog_ordering_args( $atts['orderby'], $atts['order'] );
    $meta_query    = WC()->query->get_meta_query();
    $query_args    = array( 
        'post_type'           => 'product',
        'post_status'         => 'publish',
        'ignore_sticky_posts' => 1,
        'orderby'             => $ordering_args['orderby'],
        'order'               => $ordering_args['order'],
        'posts_per_page'      => $atts['per_page'],
        'meta_query'          => $meta_query
    );

    $query_args = self::_maybe_add_vendor_args( $query_args, $atts['vendor'], $atts['operator'] );

    if ( isset( $ordering_args['meta_key'] ) ) {
        $query_args['meta_key'] = $ordering_args['meta_key'];
    }

    $return = self::product_loop( $query_args, $atts, 'product_cat' );

    // Remove ordering query arguments
    WC()->query->remove_ordering_args();

    return $return;
} 

(…)

    private static function _maybe_add_vendor_args( $args, $vendor, $operator ) {
    if ( ! empty( $vendor ) ) {
        $args['tax_query'] = array(
            array(
                'taxonomy' => 'yith_shop_vendor',
                'terms'    => array_map( 'sanitize_title', explode( ',', $vendor ) ),
                'field'    => 'slug',
                'operator' => $operator
            )
        );
    }

    return $args;
}

Read more here: Create a plugin with new shortcodes derived from WooCommerce


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: