更改 Woocommerce 简码上的产品元查询
Change product meta query on Woocommerce shortcodes
我在一家 woocommerce 商店工作,我必须在其中展示位置自定义值的产品。产品属于任何位置。我已经完成了将近 50% 的插件,商店页面上的产品得到了完美的过滤,但特色产品、销售产品、最近的产品等没有得到过滤,因为它们是从 woocommerce 产品简码生成的。
到目前为止,我在管理产品页面中添加了一个自定义文件,并在商店页面的前端显示过滤产品。现在我想从短代码中过滤产品。
我在管理站点的产品信息页面中添加了自定义字段,下面是代码:
/* adding custom product field: location */
function add_custom_product_text_filed(){
global $woocommerce, $post;
$locatons = array('all' => 'All');
$locations_posts = get_posts(array('post_type' => 'location', 'numberposts' => -1));
foreach ($locations_posts as $loc_post) {
$locatons[$loc_post->ID] = $loc_post->post_title;
}
echo '<div class="options_group">';
woocommerce_wp_select(
array(
'id' => '_location',
'label' => __('Location'),
'desc_tip' => true,
'description' => __('Enter the product location here'),
'options' => $locatons,
)
);
echo '</div>';
}
下面是根据位置显示产品的代码
/*Get Location Based Products*/
add_action( 'woocommerce_product_query', 'location_products' );
function location_products($q){
if (isset($_COOKIE['wc_location_product_id']) && !empty($_COOKIE['wc_location_product_id']) && !is_null($_COOKIE['wc_location_product_id']) && $_COOKIE['wc_location_product_id'] != 'all') {
$meta_query = $q->get('meta_query');
$meta_query[] = array(
'key' => '_location',
'value' => $_COOKIE['wc_location_product_id'],
'compare' => '=',
);
$q->set('meta_query', $meta_query);
}
}
现在我想根据自定义提交的位置更改 woocommerce 产品简码(销售产品、特色产品、最新产品等)的工作,因为页面构建器使用简码来显示产品,但我不知道如何操作我可以改变简码功能的方式。是否有任何钩子或过滤器可以完成此任务,或者有任何示例可以说明如何执行此操作。感谢您的帮助。
谢谢。
以下使用 woocommerce_shortcode_products_query
专用过滤器挂钩的代码允许更改 Woocommerce 简码上的产品查询:
add_filter( 'woocommerce_shortcode_products_query', 'shortcode_products_query_on_location', 10, 3 );
function shortcode_products_query_on_location( $query_args, $atts, $loop_name ){
if (isset($_COOKIE['wc_location_product_id']) && !empty($_COOKIE['wc_location_product_id']) && !is_null($_COOKIE['wc_location_product_id']) && $_COOKIE['wc_location_product_id'] != 'all') {
$query_args['meta_query'] = array( array(
'key' => '_location',
'value' => $_COOKIE['wc_location_product_id'],
'compare' => '=',
) );
}
return $query_args;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。它应该可以工作。
我在一家 woocommerce 商店工作,我必须在其中展示位置自定义值的产品。产品属于任何位置。我已经完成了将近 50% 的插件,商店页面上的产品得到了完美的过滤,但特色产品、销售产品、最近的产品等没有得到过滤,因为它们是从 woocommerce 产品简码生成的。
到目前为止,我在管理产品页面中添加了一个自定义文件,并在商店页面的前端显示过滤产品。现在我想从短代码中过滤产品。
我在管理站点的产品信息页面中添加了自定义字段,下面是代码:
/* adding custom product field: location */
function add_custom_product_text_filed(){
global $woocommerce, $post;
$locatons = array('all' => 'All');
$locations_posts = get_posts(array('post_type' => 'location', 'numberposts' => -1));
foreach ($locations_posts as $loc_post) {
$locatons[$loc_post->ID] = $loc_post->post_title;
}
echo '<div class="options_group">';
woocommerce_wp_select(
array(
'id' => '_location',
'label' => __('Location'),
'desc_tip' => true,
'description' => __('Enter the product location here'),
'options' => $locatons,
)
);
echo '</div>';
}
下面是根据位置显示产品的代码
/*Get Location Based Products*/
add_action( 'woocommerce_product_query', 'location_products' );
function location_products($q){
if (isset($_COOKIE['wc_location_product_id']) && !empty($_COOKIE['wc_location_product_id']) && !is_null($_COOKIE['wc_location_product_id']) && $_COOKIE['wc_location_product_id'] != 'all') {
$meta_query = $q->get('meta_query');
$meta_query[] = array(
'key' => '_location',
'value' => $_COOKIE['wc_location_product_id'],
'compare' => '=',
);
$q->set('meta_query', $meta_query);
}
}
现在我想根据自定义提交的位置更改 woocommerce 产品简码(销售产品、特色产品、最新产品等)的工作,因为页面构建器使用简码来显示产品,但我不知道如何操作我可以改变简码功能的方式。是否有任何钩子或过滤器可以完成此任务,或者有任何示例可以说明如何执行此操作。感谢您的帮助。 谢谢。
以下使用 woocommerce_shortcode_products_query
专用过滤器挂钩的代码允许更改 Woocommerce 简码上的产品查询:
add_filter( 'woocommerce_shortcode_products_query', 'shortcode_products_query_on_location', 10, 3 );
function shortcode_products_query_on_location( $query_args, $atts, $loop_name ){
if (isset($_COOKIE['wc_location_product_id']) && !empty($_COOKIE['wc_location_product_id']) && !is_null($_COOKIE['wc_location_product_id']) && $_COOKIE['wc_location_product_id'] != 'all') {
$query_args['meta_query'] = array( array(
'key' => '_location',
'value' => $_COOKIE['wc_location_product_id'],
'compare' => '=',
) );
}
return $query_args;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。它应该可以工作。