如何仅显示 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>';
快照如下:
See The screenshot here
这显示了我之前存储的所有属性。
有什么解决方案,如果有人可以帮助我?
试试这个
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>';
}
我不想显示所有可用的属性,我只需要显示那些在后端保存有库存的变体值。 就像我有一个名为尺寸的变量,所以我想显示产品及其可用尺寸,如果产品有 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>';
快照如下: See The screenshot here 这显示了我之前存储的所有属性。 有什么解决方案,如果有人可以帮助我?
试试这个
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>';
}