【问题标题】:Product category filter displaying a list of products with their stock in Woocommerce产品类别过滤器显示产品列表及其在 Woocommerce 中的库存
【发布时间】:2018-09-20 10:10:52
【问题描述】:

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

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

我遇到的问题似乎是表单将数据发送到查询。

如果我为类别手动编码 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

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

任何帮助将不胜感激,谢谢

我的代码:

<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> 

【问题讨论】:

    标签: php jquery html wordpress woocommerce


    【解决方案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>
    

    经过测试并且有效。

    【讨论】:

    • 感谢您的额外调整,它们有很大的不同
    猜你喜欢
    • 2012-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-14
    • 2018-06-14
    相关资源
    最近更新 更多