Php 隐藏子产品,使其不受网络上分组产品的影响
让分配给分组产品的所有单个产品都可用并在归档/类别页面上可见不是一个理想的解决方案,我想知道如何解决这个问题 我知道在WooCommerce中有一个“可见性”选项,但这更不理想 据我所知,WooCommerce现在对此使用Php 隐藏子产品,使其不受网络上分组产品的影响,php,wordpress,woocommerce,metadata,product,Php,Wordpress,Woocommerce,Metadata,Product,让分配给分组产品的所有单个产品都可用并在归档/类别页面上可见不是一个理想的解决方案,我想知道如何解决这个问题 我知道在WooCommerce中有一个“可见性”选项,但这更不理想 据我所知,WooCommerce现在对此使用元数据,而不是post\u parent,因此,我请求有关如何更新此查询以涵盖此内容的帮助 我尝试过的代码不再有效: add_action( 'woocommerce_product_query', 'hide_single_products_assigned_to_group
元数据
,而不是post\u 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\u post\u meta
表的\u children
meta\u键下,作为序列化数组
但您可以做的是首先向分组产品中的所有子产品添加一个自定义字段。然后,您将能够使用该自定义字段更改产品查询
以下函数将执行该作业,您将只运行一次:
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 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 );
}
}
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文件。测试和工作
现在结束,将在所有产品上隐藏的函数将从分组产品中循环子产品:
// 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 );
}
}
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文件。测试和工作
相关:在运行函数三次后,更改“开始”。非常感谢您花时间查看并修复此问题。我真的很感激。