产品类别过滤器显示的产品列表以及它们在Woocommerce股票(Product category

2019-09-30 01:20发布

我试着去创建一个股票报告我的商店主要是用于“可变”和一些“简单”的产品。

我们的想法是,用户选择从下拉列表中选择类别和页面刷新,显示对所选类别的股票。

遇到的问题IM似乎与表单中的数据发送到查询。

如果我手工编码蛞蝓的类别我想一切正常变量和简单的产品。 然而,当我尝试实施形式发布的类别我开始得到以下调试错误的查询。

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

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

测试和工程。



文章来源: Product category filter displaying a list of products with their stock in Woocommerce