产品类别filter在Woocommerce中显示其库存产品列表

我试图为我的商店创建一个股票报告,主要是“变量”和一些“简单”的产品。

这个想法是用户从下拉列表中选择类别,页面刷新显示所选类别的库存。

我遇到的问题似乎与将数据发送到查询的forms有关。

如果我手动编写类别的slug我希望一切都适用于可变和简单的产品。 但是,当我尝试实现表单将类别发布到查询时,我开始在下面获取调试错误。

[20-Sep-2018 09:52:42 UTC] PHP Fatal error: Uncaught Error: Call to undefined method WC_Product_Simple::get_available_variations() in C:\wamp64\www\devbb.co.uk\wp-content\themes\bb-theme\page-stock.php:79 Stack trace: #0 C:\wamp64\www\devbb.co.uk\wp-includes\template-loader.php(74): include() #1 C:\wamp64\www\devbb.co.uk\wp-blog-header.php(19): require_once('C:\\wamp64\\www\\d...') #2 C:\wamp64\www\devbb.co.uk\index.php(17): require('C:\\wamp64\\www\\d...') #3 {main} thrown in C:\wamp64\www\devbb.co.uk\wp-content\themes\bb-theme\page-stock.php on line 79 

我真正不理解的部分是,当我在自己编写类别时一切正常但是当表单试图传递相同的数据时,所有错误都会出现?

任何帮助将非常感谢,谢谢

我的代码:

  
"product_cat", 'orderby' => 'slug', 'order' => 'ASC', 'hide_empty' => 1, ); $cats_select_list = get_terms( 'product_cat', $cat_args ); foreach ($cats_select_list as $select_list){ //if ( strpos($select_list->slug, 'express') || ( strpos($select_list->slug, 'clearance') ) === false) { echo 'slug . '">' . str_replace ('-', ' ', $select_list->slug) . ''; //} } ?>
10, 'orderby' => 'title', 'order' => 'ASC', 'return' => 'ids', //'category' => 'my-hockey-club-clearance', 'category' => $club_cat, ) ); $products = $query->get_products(); foreach ($products as $prod) { $actual = wc_get_product( $prod ); $variations = $actual->get_available_variations(); foreach ($variations as $key => $value) { echo ''; echo ''; echo ''; echo ''; echo ''; } } ?>
get_sku() . '" href="' . get_permalink($actual->get_id()) . '">' . $actual->get_name() . ''; foreach ($value['attributes'] as $attr_key => $attr_value) { $prefix = 'attribute_pa_'; $str = $attr_key; if (substr($str, 0, strlen($prefix)) === $prefix) { $str = substr($str, strlen($prefix)); } echo ''; echo ''; echo ''; echo ''; echo ''; echo '
' . $str . '' . $attr_value . '
'; } echo '
' . $value['availability_html'] . '
$(function() { $('#cat-select-box').on('change', function(e) { $(this).closest('form') .trigger('submit') }) })

自Woocommerce 3以来,您的代码中存在一些错误……我还做了一些补充: – 在重新加载项目时保留所选菜单项, – 添加了标记的启动选项。

请尝试以下方法:

 
10, 'orderby' => 'title', 'order' => 'ASC', 'category' => $club_cat, ) ); foreach ($products as $product) { if( $product->is_type('variable')){ foreach ($product->get_available_variations() as $values ) { echo ''; } } } ?>
' . get_the_title($values['variation_id']) . ' '; foreach ($values['attributes'] as $attribute => $term_slug) { $taxonomy = str_replace('attribute_', '', $attribute); $attr_name = get_taxonomy( $taxonomy )->labels->singular_name; // Attribute name $term_name = get_term_by( 'slug', $term_slug, $taxonomy )->name; // Term name echo '
' . $attr_name . ' ' . $term_name . '
'; } echo '
' . $values['availability_html'] . '

经过测试和工作。