WCFM Marketplace for WooCommerce – Ignore current product

I am using WCFM Marketplace for WooCommerce plugin and am building the single product page in Elementor theme. I would like to display the vendors products and WCFM include a shortcode of [products store=”id”] which is part of the standard WooCommerce [products] shortcode.

I asked WCFM developers if there was a way of dynamically adding the store ID to the shortcode and they provided this code:

add_shortcode('wcfm_store_related_products','fn_wcfm_store_related_products');
function fn_wcfm_store_related_products($attr) {
    global $WCFM, $WCFMmp, $wp, $WCFM_Query, $post;     
    $store_id = '';
    if ( isset( $attr['id'] ) && !empty( $attr['id'] ) ) { $store_id = absint($attr['id']); }   
    if (  wcfm_is_store_page() ) {
        $wcfm_store_url = get_option( 'wcfm_store_url', 'store' );
        $store_name = apply_filters( 'wcfmmp_store_query_var', get_query_var( $wcfm_store_url ) );
        $store_id  = 0;
        if ( !empty( $store_name ) ) {
            $store_user = get_user_by( 'slug', $store_name );
        }
        $store_id           = $store_user->ID;
    }   
    if( is_product() ) {
        $store_id = $post->post_author;
    }
    if( !$store_id && is_single() && $post && is_object( $post ) && wcfm_is_vendor( $post->post_author ) ) {
        $store_id = $post->post_author;
    }
    echo do_shortcode('[products columns="5" limit="10" store="'.$store_id.'"]');
}

This works, however it also displays the current product when added to the single product page.

What I would like to do is ignore the current product (do not show it). Does anyone know how I would go about this?

Answer

I don’t know if WC products have excluded params for products or not but you can use WC woocommerce_shortcode_products_query filter hook that you can change params as you want in your case check below code.

function remove_current_prodcut_woocommerce_shortcode_products_query( $query_args, $attributes, $type ){
    global $post;
    if( is_product() ){
        $query_args['post__not_in'] = array($post->ID);
    }
    return $query_args;
}
add_filter( 'woocommerce_shortcode_products_query', 'remove_current_prodcut_woocommerce_shortcode_products_query', 10, 3 );

Leave a Reply

Your email address will not be published. Required fields are marked *