在 Woocommerce 循环中隐藏分组产品中的子产品
Hide the children products from grouped product on Woocommerce loops
让所有分配给分组产品的单一产品在存档/类别页面上可用并可见并不是一个理想的解决方案,我想知道如何解决这个问题。
我知道 WooCommerce 中有一个“可见性”选项,但这更不理想。
据我了解,WooCommerce 现在为此使用 meta data
而不是 post_parent
,因此,我正在寻求有关如何更新此查询以涵盖该查询的帮助。
我试过但不再有效的代码 :
add_action( 'woocommerce_product_query', 'hide_single_products_assigned_to_grouped_product_from_archive' );
function hide_single_products_assigned_to_grouped_product_from_archive( $q ){
$q->set( 'post_parent', 0 );
}
您无法在产品查询中真正定位分组产品中的子产品,因为数据存储在 wp_post_meta
table _children
meta_key 下作为序列化数组。
但是您可以做的是首先向分组产品中的所有子产品添加一个自定义字段。然后您将能够使用该自定义字段来更改产品查询。
以下函数将完成这项工作,您将 运行 它 仅一次 :
function add_a_custom_field_to_grouped_children_products() {
// get all grouped products Ids
$grouped_ids = wc_get_products( array( 'limit' => -1, 'type' => 'grouped', 'return' =>'ids' ) );
// Loop through grouped products
foreach( $grouped_ids as $grouped_id ){
// Get the children products ids
$children_ids = (array) get_post_meta( $grouped_id, '_children', true );
// Loop through children product Ids
foreach( $children_ids as $child_id ) {
// add a specific custom field to each child with the parent grouped product id
update_post_meta( $child_id, '_child_of', $grouped_id );
}
}
}
add_a_custom_field_to_grouped_children_products(); // Run the function
代码进入您的活动子主题(或活动主题)的 functions.php 文件。
保存后,浏览您网站的任何页面。然后删除该代码并保存。
现在您所有分组的子产品都将有一个自定义字段。如果您 add/create 更多分组产品,您将需要以下功能来将自定义字段添加到子产品中:
// Add on the children products from a grouped product a custom field
add_action( 'woocommerce_process_product_meta_grouped', 'wc_action_process_children_product_meta' );
function wc_action_process_children_product_meta( $post_id ) {
// Get the children products ids
$children_ids = (array) get_post_meta( $post_id, '_children', true );
// Loop through children product Ids
foreach( $children_ids as $child_id ) {
// add a specific custom field to each child with the parent grouped product id
update_post_meta( $child_id, '_child_of', $post_id );
}
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
现在要完成,将在所有产品循环中隐藏分组产品中的子产品的功能:
add_filter( 'woocommerce_product_query_meta_query', 'hide_children_from_grouped_products' );
function hide_children_from_grouped_products( $meta_query ) {
if( ! is_admin() ) {
$meta_query[] = array(
'key' => '_child_of',
'compare' => 'NOT EXISTS'
);
}
return $meta_query;
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
相关:
让所有分配给分组产品的单一产品在存档/类别页面上可用并可见并不是一个理想的解决方案,我想知道如何解决这个问题。
我知道 WooCommerce 中有一个“可见性”选项,但这更不理想。
据我了解,WooCommerce 现在为此使用 meta data
而不是 post_parent
,因此,我正在寻求有关如何更新此查询以涵盖该查询的帮助。
我试过但不再有效的代码
add_action( 'woocommerce_product_query', 'hide_single_products_assigned_to_grouped_product_from_archive' );
function hide_single_products_assigned_to_grouped_product_from_archive( $q ){
$q->set( 'post_parent', 0 );
}
您无法在产品查询中真正定位分组产品中的子产品,因为数据存储在 wp_post_meta
table _children
meta_key 下作为序列化数组。
但是您可以做的是首先向分组产品中的所有子产品添加一个自定义字段。然后您将能够使用该自定义字段来更改产品查询。
以下函数将完成这项工作,您将 运行 它 仅一次 :
function add_a_custom_field_to_grouped_children_products() {
// get all grouped products Ids
$grouped_ids = wc_get_products( array( 'limit' => -1, 'type' => 'grouped', 'return' =>'ids' ) );
// Loop through grouped products
foreach( $grouped_ids as $grouped_id ){
// Get the children products ids
$children_ids = (array) get_post_meta( $grouped_id, '_children', true );
// Loop through children product Ids
foreach( $children_ids as $child_id ) {
// add a specific custom field to each child with the parent grouped product id
update_post_meta( $child_id, '_child_of', $grouped_id );
}
}
}
add_a_custom_field_to_grouped_children_products(); // Run the function
代码进入您的活动子主题(或活动主题)的 functions.php 文件。
保存后,浏览您网站的任何页面。然后删除该代码并保存。
现在您所有分组的子产品都将有一个自定义字段。如果您 add/create 更多分组产品,您将需要以下功能来将自定义字段添加到子产品中:
// Add on the children products from a grouped product a custom field
add_action( 'woocommerce_process_product_meta_grouped', 'wc_action_process_children_product_meta' );
function wc_action_process_children_product_meta( $post_id ) {
// Get the children products ids
$children_ids = (array) get_post_meta( $post_id, '_children', true );
// Loop through children product Ids
foreach( $children_ids as $child_id ) {
// add a specific custom field to each child with the parent grouped product id
update_post_meta( $child_id, '_child_of', $post_id );
}
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
现在要完成,将在所有产品循环中隐藏分组产品中的子产品的功能:
add_filter( 'woocommerce_product_query_meta_query', 'hide_children_from_grouped_products' );
function hide_children_from_grouped_products( $meta_query ) {
if( ! is_admin() ) {
$meta_query[] = array(
'key' => '_child_of',
'compare' => 'NOT EXISTS'
);
}
return $meta_query;
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
相关: