亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如果所有變體都缺貨,則從目錄中隱藏 WooCommerce 可變產品

如果所有變體都缺貨,則從目錄中隱藏 WooCommerce 可變產品

PHP
HUX布斯 2023-09-08 10:29:56
在我的 WooCommerce 商店中,有一些顏色各異的產品。庫存的更新是變化級別的,因為外部源將每種顏色視為不同的產品?,F在我遇到一個問題,可變產品的所有變體都缺貨,但產品本身仍然顯示在網上商店的目錄中。即使我打開設置“隱藏目錄中缺貨的商品”。但此設置僅適用于沒有變體的產品。當您點擊某個型號全部缺貨的產品時,它會顯示:“該產品目前缺貨且不可用?!?。WooCommerce 中是否有一個設置也可以隱藏我的目錄中的這些產品?或者我是否必須制作一個額外的腳本來檢查每個可變產品并將其置于最高水平缺貨?我在可變產品上嘗試過的代碼沒有效果:$out_of_stock_staus = 'outofstock';// 1. Updating the stock quantityupdate_post_meta($product_id, '_stock', 0);// 2. Updating the stock statusupdate_post_meta( $product_id, '_stock_status', wc_clean( $out_of_stock_staus ) );// 3. Updating post term product visibilitywp_set_post_terms( $product_id, $out_of_stock_staus, 'product_visibility', true ); 如果所有變體都缺貨,如何從目錄中隱藏 WooCommerce 可變產品?
查看完整描述

1 回答

?
桃花長相依

TA貢獻1860條經驗 獲得超8個贊

注意:從 WooCommerce 3 和 4 開始,最好使用WC_Product?可用的方法,因為有新的自定義表和緩存數據需要更新。

有一種方法可以顯示或隱藏產品目錄中的可變產品(有點復雜)

1)。一個輕量級條件函數,用于檢查可變產品的所有產品變體是否“缺貨”:

// Conditional function to check if a variable product has at least a variation in stock

function is_wc_variable_product_in_stock( $product_id ){

? ? global $wpdb;


? ? $count = $wpdb->get_var( $wpdb->prepare( "

? ? ? ? SELECT COUNT(ID)

? ? ? ? FROM {$wpdb->posts} p

? ? ? ? INNER JOIN {$wpdb->postmeta} pm

? ? ? ? ? ? ON p.ID? ? ? ? ? ?=? pm.post_id

? ? ? ? WHERE p.post_type? ? ?=? 'product_variation'

? ? ? ? ? ? AND p.post_status =? 'publish'

? ? ? ? ? ? AND p.post_parent =? %d

? ? ? ? ? ? AND pm.meta_key? ?=? '_stock_status'

? ? ? ? ? ? AND pm.meta_value != 'outofstock'

? ? ", $product_id ) );


? ? return $count > 0 ? true : false;

}

代碼位于活動子主題(或活動主題)的 function.php 文件中。下面需要這個函數,所以保留它。


2)。一個自定義函數,將檢查所有可變產品(及其所有變體庫存),添加自定義元數據(此函數將僅使用一次,然后刪除)。


// Function that will check the variations stock for each variable products adding custom meta data

function check_and_update_all_variable_products(){

? ? // Only for admins

? ? if( ! current_user_can('edit_products')) return;


? ? // get all variable product Ids

? ? $variable_products_ids = wc_get_products( array(

? ? ? ? 'limit'? => -1,

? ? ? ? 'status' => 'publish',

? ? ? ? 'type'? ?=> 'variable',

? ? ? ? 'return' => 'ids',

? ? ));


? ? // Loop through variable products

? ? foreach( $variable_products_ids as $variable_id ) {

? ? ? ? // Check if all the product variations are "out of stock" or not

? ? ? ? $value? ? ? = is_wc_variable_product_in_stock( $variable_id ) ? '0' : '1';


? ? ? ? $meta_value = (string) get_post_meta( $variable_id, '_all_variations_outofstock', true );


? ? ? ? if ( $value !== $meta_value ) {

? ? ? ? ? ? // Create/Udpate custom meta data

? ? ? ? ? ? update_post_meta( $variable_id, '_all_variations_outofstock', $value );

? ? ? ? }

? ? }

}

// Run the function: browse any page of your web site

check_and_update_all_variable_products();

代碼位于活動子主題(或活動主題)的 function.php 文件中。保存后,瀏覽您網站的任何頁面即可運行此功能。然后將其刪除。


3)?,F在,一個鉤子函數將隱藏您的變量產品,這些產品的變體都是“缺貨”,來自 WooCommerce 目錄和單個產品頁面:


// Hide product variations which variations are all "out of stock"

add_action( 'woocommerce_product_query', 'hide_variable_products_with_all_outofstock_variations', 10, 2 );

function hide_variable_products_with_all_outofstock_variations( $q, $query ) {

? ? // Get any existing meta query

? ? $meta_query = $q->get( 'meta_query');


? ? // Define an additional meta query

? ? $meta_query['relation'] = 'OR';

? ? $meta_query[] = array(

? ? ? ? 'key'? ? ?=> '_all_variations_outofstock',

? ? ? ? 'value'? ?=> '1',

? ? ? ? 'compare' => '!=',

? ? );

? ? $meta_query[] = array(

? ? ? ? 'key'? ? ?=> '_all_variations_outofstock',

? ? ? ? 'compare' => 'NOT EXISTS',

? ? );


? ? // Set the new merged meta query

? ? $q->set( 'meta_query', $meta_query );

}


// Hide "Out of stock" variable product single pages

add_action( 'template_redirect', 'hide_out_of_stock_variable_product_single_pages' );

function hide_out_of_stock_variable_product_single_pages(){

? ? if( get_post_meta( get_the_id(), '_all_variations_outofstock', true ) ) {

? ? ? ? // Redirect to Shop page

? ? ? ? wp_redirect( wc_get_page_permalink( 'shop' ) );

? ? ? ? exit();

? ? }

}

代碼位于活動子主題(或活動主題)的 function.php 文件中。經過測試并有效。


4). 添加/更新產品變體庫存自定義元:在管理單一產品頁面上,檢查可變產品庫存狀態并更新自定義元數據(以及當付款訂單更新產品庫存時):


// Custom function that update the parent variable product stock custom meta data

function update_parent_variable_product_stock_custom_meta( $product_var ) {

? ? if( ! is_a($product_var, 'WC_Product') ) {

? ? ? ? $product = wc_get_product( $product_var );

? ? } else { $product = $product_var; }


? ? if ( $product->is_type('variation') ) {

? ? ? ? // Get the parent product id from product variation

? ? ? ? $product_id = (int) $product->get_parent_id();

? ? } elseif ( $product->is_type('variable') ) {

? ? ? ? $product_id = (int) $product->get_id();

? ? } else {

? ? ? ? return; // Exit

? ? }


? ? // Check if all the product variations are "out of stock" or not

? ? $value = is_wc_variable_product_in_stock( $product_id ) ? '0' : '1';


? ? // add/Udpate custom meta data

? ? update_post_meta( $variable_id, '_all_variations_outofstock', $value );

}


// Update variable product stock custom meta data on admin single product pages

add_action( 'woocommerce_process_product_meta_variable', 'update_variable_product_stock_custom_meta_data' );

function update_product_variable_stock_custom_meta_data ( $product_id ) {

? ? update_parent_variable_product_stock_custom_meta( $product_id );

}


// On processed update product stock event (Like on orders)

add_action( 'woocommerce_updated_product_stock', 'wc_updated_product_stock_callback' );

function wc_updated_product_stock_callback ( $product_id ) {

? ? update_parent_variable_product_stock_custom_meta( $product_id );

}

代碼位于活動子主題(或活動主題)的 function.php 文件中。經過測試并有效。


5)。以編程方式更改產品庫存狀態并添加/更新產品變體庫存自定義元(您重新訪問的代碼,但適用于產品變體甚至簡單的產品):


$stock_status = 'outofstock';


// Get an instance of WC_Product Object from the product ID (product variation)

$product = wc_get_product( $product_id );


// 1. Updating the stock quantity

$product->set_stock_quantity(0);


// 2. Updating the stock status (and "product_visibility" related term )

$product->set_stock_status($stock_status);


// 3. Save product data and refresh cached data

$product->save();


// Update parent variable product stock custom meta data

update_parent_variable_product_stock_custom_meta( $product );


查看完整回答
反對 回復 2023-09-08
  • 1 回答
  • 0 關注
  • 160 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號