第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

隱藏 Woocommerce 循環(huán)中分組產(chǎn)品中的兒童產(chǎn)品

隱藏 Woocommerce 循環(huán)中分組產(chǎn)品中的兒童產(chǎn)品

PHP
婷婷同學(xué)_ 2023-08-26 09:53:49
讓分配給分組產(chǎn)品的所有單一產(chǎn)品在存檔/類別頁面上可用并可見并不是一個(gè)理想的解決方案,我想知道如何解決這個(gè)問題。我知道 WooCommerce 中有一個(gè)“可見性”選項(xiàng),但這更不理想。據(jù)我了解,WooCommerce 現(xiàn)在用于meta data此用途,而不是post_parent用于此目的,因此,我請(qǐng)求幫助以了解如何更新此查詢以涵蓋該查詢。我嘗試過的這里的代碼不再起作用: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 );}
查看完整描述

1 回答

?
阿波羅的戰(zhàn)車

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超6個(gè)贊

您無法真正在產(chǎn)品查詢中定位分組產(chǎn)品中的子產(chǎn)品,因?yàn)閿?shù)據(jù)作為序列化數(shù)組存儲(chǔ)_children在表上的 meta_key下。wp_post_meta


但您可以做的是首先向分組產(chǎn)品中的所有子產(chǎn)品添加自定義字段。然后您將能夠使用該自定義字段來更改產(chǎn)品查詢。


以下函數(shù)將完成該工作,并且您只需運(yùn)行它一次:


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

代碼位于活動(dòng)子主題(或活動(dòng)主題)的functions.php 文件中。


保存后,瀏覽您網(wǎng)站的任何頁面。然后刪除該代碼并保存。


現(xiàn)在,所有分組的兒童產(chǎn)品都將有一個(gè)自定義字段。如果您添加/創(chuàng)建更多分組產(chǎn)品,您將需要以下函數(shù)來將該自定義字段添加到子產(chǎn)品中:


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

? ? }

}

代碼位于活動(dòng)子主題(或活動(dòng)主題)的functions.php 文件中。經(jīng)過測(cè)試并有效。


現(xiàn)在完成,將隱藏所有產(chǎn)品的函數(shù)循環(huán)分組產(chǎn)品中的子產(chǎn)品:


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;

}

代碼位于活動(dòng)子主題(或活動(dòng)主題)的functions.php 文件中。經(jīng)過測(cè)試并有效。

查看完整回答
反對(duì) 回復(fù) 2023-08-26
  • 1 回答
  • 0 關(guān)注
  • 141 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)