问题描述
我正在使用 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 );