产品类别过滤器显示Woocommerce中具有其库存的产品列表

时间:2018-09-20 10:10:53

标签: php jquery html wordpress woocommerce

我正在尝试为我的商店创建一个库存报告,该报告主要用于“可变”产品和一些“简单”产品。

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

问题似乎在于将数据发送到查询的表单。

如果我为该类别的段子手动编码,我希望对于可变和简单产品来说一切正常。但是,当我尝试实现将类别发布到查询中的表单时,开始出现以下调试错误。

[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

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

非常感谢您的帮助,

我的代码:

<main>
         <form id="test" name="test1" method="post">
         <select id="cat-select-box" name="amt_per">

                <?php
                $cat_args = array(
                    'taxonomy'   => "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 '<option class="amt-button" name="amt_per" value="' . $select_list->slug . '">' . str_replace ('-', ' ', $select_list->slug) . '</option>';
                    //} 
                }
                ?>
                    </select>
                </form>

                <table id="fx_stock_manager">
                        <?php

                        $default = 'my-hockey-club-clearance';

                        $club_cat = isset($_POST['amt_per'])? $_POST['amt_per']: $default;

                        $query = new WC_Product_Query( array(
                            //'limit' => 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 '<tr>';

                                echo '<td><a title="' . $actual->get_sku() . '" href="' . get_permalink($actual->get_id()) . '">' . $actual->get_name() . '</a></td>';

                                echo '<td>';

                                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 '<table>';
                                        echo '<tr>';
                                            echo '<td>' . $str . '</td>';
                                            echo '<td>' . $attr_value . '</td>';
                                        echo '</tr>';
                                    echo '</table>';   
                                }
                                echo '</td>';
                                echo '<td class="fx_stock_count">' . $value['availability_html'] . '</td>';
                                echo '</tr>';
                                }
                            }
                          ?>
                </table>

<script>
    $(function() {
        $('#cat-select-box').on('change', function(e) {
            $(this).closest('form')
                .trigger('submit')
        })
    })
</script> 

1 个答案:

答案 0 :(得分:1)

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

请尝试以下操作:

<main>
    <form id="test" name="test1" method="post">
        <select id="cat-select-box" name="amt_per">
            <option class="amt-button" value=""'.$selected.'><?php _e("Chose a category"); ?></option>

        <?php
        $product_categories = get_terms( array(
            'taxonomy'   => "product_cat",
            'orderby'    => 'slug',
            'order'      => 'ASC',
            'hide_empty' => 1,
        ));

        foreach ($product_categories as $term ){
            $selected = isset($_POST['amt_per']) && $_POST['amt_per'] == $term->slug ? ' selected' : '';
            echo '<option class="amt-button" value="' . $term->slug . '"'.$selected.'>' . $term->name . '</option>';
        }
        ?>
        </select>
    </form>
    <table id="fx_stock_manager">
    <?php
    $club_cat = isset($_POST['amt_per'])? $_POST['amt_per']: 'my-hockey-club-clearance';
    $products = wc_get_products( array(
        //'limit' => 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 '<tr><td><a title="' . $values['sku'] . '" href="' . get_permalink($values['variation_id']) . '">' . get_the_title($values['variation_id']) . '</a></td>
                <td>';

                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 '<table>
                    <tr>
                        <td>' . $attr_name . '</td>
                        <td>' . $term_name . '</td>
                    </tr>
                    </table>';
                }
                echo '</td>
                    <td class="fx_stock_count">' . $values['availability_html'] . '</td>
                </tr>';
            }
        }
    }
?>
</table>
<script>
    jQuery(function($) {
        $('#cat-select-box').on('change', function() {
            if( $(this).val() != '0' )
                $(this).closest('form').trigger('submit')
        })
    })
</script>

经过测试可以正常工作。