如何在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>';

这是快照:请参阅此处的屏幕截图 这显示了我之前存储的所有属性。如果有人可以帮助我,解决方案是什么?


茅侃侃
浏览 129回答 1
1回答

狐的传说

试试这个global $product;&nbsp; &nbsp; &nbsp; &nbsp; $taxonomy&nbsp; &nbsp; = 'pa_sizes'; // The product attribute taxonomy&nbsp; &nbsp; &nbsp; &nbsp; $sizes_array = []; // Initializing&nbsp; &nbsp; &nbsp; &nbsp; // Loop through available variation Ids for the variable product&nbsp; &nbsp; &nbsp; &nbsp; foreach( $product->get_children() as $child_id ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $variation = wc_get_product( $child_id ); // Get the WC_Product_Variation object&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( $variation->is_purchasable() && $variation->is_in_stock() ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $term_name = $variation->get_attribute( $taxonomy );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $file_list[$term_name] = $term_name;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Loop through the array and print all the values&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; if(is_array($file_list)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sort ($file_list);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach($file_list as $file){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<button class="btn btn-circle btn-lg rounded-circle">' . $file . '</button>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<p style="font-size:12px;">No Sizes Available</p>';&nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP