簡體   English   中英

WooCommerce:按屬性過濾產品並在變體缺貨時隱藏產品

[英]WooCommerce: filter products by attribute and hide product if variation is out of stock

我使用內置的 WooCommerce 過濾器按屬性小部件,它適用於我需要的東西,但我的問題是,如果我過濾尺寸,我會得到所有具有該尺寸的產品作為屬性,即使它們缺貨時也是如此。 .

我的產品是作為可變產品創建的,具有不同的尺寸和顏色

我想要的是過濾尺寸並僅顯示該尺寸的庫存產品。

對於可變產品及其變體(但僅適用於其他產品類型)可能是不可能的

當庫存管理由產品變體本身(而不是變體)處理時,它僅適用於可變產品。

請注意,WooCommerce 產品查詢不處理帖子類型“product_variation”,而只處理帖子類型“產品”。

要使用小部件過濾器排除缺貨產品(可變產品除外),您可以使用:

add_filter( 'woocommerce_product_query_tax_query', 'filter_product_query_tax_query' );
function filter_product_query_tax_query( $tax_query ){
    if ( ! is_admin() && isset($_GET['filter_color']) && ! empty($_GET['filter_color']) ) {
        // Exclude products "out of stock"
        $tax_query[] = array(
            'taxonomy' => 'product_visibility',
            'field'    => 'name',
            'terms'    => array('outofstock'),
            'operator' => 'NOT IN'
        );
    }

    return $tax_query;
}

代碼進入活動子主題(或活動主題)的functions.php文件。 測試和工作。

我已將此代碼用於我的商店,以在過濾時隱藏沒有庫存的產品尺寸。

add_action( 'woocommerce_before_shop_loop_item_title', 'scr_out_of_stock_variations_loop' );
function scr_out_of_stock_variations_loop(){
    global $product;
    if ( $product->is_type('variable')) { // if variation product is out of stock
        $available = $product->get_available_variations();
        if ( $available )foreach ( $available as $instockvar ) {
            if ( isset($instockvar['attributes']['attribute_pa_size'] ) ) {
                if(isset($_GET['filter_size'])){
                    $destostock = $_GET['filter_size'];
                    if($destostock ==$instockvar['attributes']['attribute_pa_size']){
                        if($instockvar['is_in_stock']){
                            global $product;
                            $id = $product->get_id();
                            echo "<style>.post-$id{display: block !important}</style>";
                        }else{
                            global $product;
                            $id = $product->get_id();
                            echo "<style>.post-$id{display: none}</style>";
                        }
                    }
                }
            }   
        }
    }
}

如果選擇了多個過濾器,filter_size 將轉換為數組。 至於產品變體,最好將其存儲在一個數組中,並使用 array_intersect 搜索所選項目之一是否有庫存。

add_filter( 'woocommerce_product_is_visible', 'hide_product_with_outofstock_variation', 10, 2 );
if(!function_exists('hide_product_with_outofstock_variation')){
    function hide_product_with_outofstock_variation( $is_visible, $id ) {
        if(isset($_GET['filter_size'])){
                $filter_size = explode(',',$_GET['filter_size']);
                $product = wc_get_product($id);
                if ( $product->is_type('variable')) { // if variation product is out of stock
                    $available = $product->get_available_variations();
                    $available_size = array();

                    if ( $available )foreach ( $available as $instockvar ) {
                        array_push($available_size, $instockvar['attributes']['attribute_pa_size']);
                    }
                    if (count(array_intersect($filter_size, $available_size)) === 0) {
                        $is_visible = false;
                    }else{
                        $is_visible = true;
                    }
                }
        }
        return $is_visible;
    }
}

這段代碼對我有用

您可以通過檢查設置頁面上的“缺貨可見性”選項來隱藏缺貨產品:

Woocommerce > 設置 > 產品 > 庫存

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM