簡體   English   中英

如何僅顯示woocommerce商店頁面上有貨的變體?

[英]How to show variations only which are in stock on shop page in woocommerce?

我不想顯示所有可用的屬性,我只需要顯示那些在后端保存庫存的變化值。 就像我有一個變量命名的尺寸,所以我想顯示產品及其可用尺寸,如果產品有 5 種不同尺寸,但其中兩個尺寸沒有庫存,則僅顯示其他 3 個有庫存的尺寸,如果一種產品有 5 種不同的尺寸並且所有尺寸都有庫存,則顯示所有這些尺寸。 這是我正在使用的代碼:

    echo '<div class="row m-0 justify-content-center">';
    $fabric_values = get_the_terms( $product->id, 'pa_sizes');
    foreach ( $fabric_values as $fabric_value ) {
        echo '<button class="btn btn-circle btn-lg rounded-circle">'."$fabric_value->name" . '</button>';
    }
    echo '</div>';

這是快照:請參閱此處的屏幕截圖這顯示了我之前存儲的所有屬性。 有什么解決辦法,如果有人可以幫助我?

嘗試這個

global $product;

        $taxonomy    = 'pa_sizes'; // The product attribute taxonomy
        $sizes_array = []; // Initializing
        // Loop through available variation Ids for the variable product
        foreach( $product->get_children() as $child_id ) {
            $variation = wc_get_product( $child_id ); // Get the WC_Product_Variation object
            if( $variation->is_purchasable() && $variation->is_in_stock() ) {
                $term_name = $variation->get_attribute( $taxonomy );
                $file_list[$term_name] = $term_name;

            }
        }

        /*
         * Loop through the array and print all the values
         */
        if(is_array($file_list)){
            sort ($file_list);
            foreach($file_list as $file){ 
                echo '<button class="btn btn-circle btn-lg rounded-circle">' . $file . '</button>';
            }
        }
        else{
            echo '<p style="font-size:12px;">No Sizes Available</p>';
        }

暫無
暫無

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

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