WooCommerce 的 WCFM 市场 - 忽略当前产品

WCFM Marketplace for WooCommerce - Ignore current product

我正在使用 WCFM Marketplace for WooCommerce 插件并在 Elementor 主题中构建单个产品页面。我想显示供应商的产品,WCFM 包含 [products store="id"] 的简码,它是标准 WooCommerce [products] 简码的一部分。

我问 WCFM 开发人员是否有一种方法可以将商店 ID 动态添加到简码中,他们提供了以下代码:

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.'"]');
}

这是有效的,但是当添加到单个产品页面时它也会显示当前产品。

我想做的是忽略当前产品(不显示)。有谁知道我会怎么做?

我不知道 WC 产品是否有 excluded 产品参数,但您可以使用 WC woocommerce_shortcode_products_query 过滤器挂钩,您可以根据需要更改参数,检查下面的代码.

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 );