产品类别过滤器显示Woocommerce中具有其库存的产品列表 [英] Product category filter displaying a list of products with their stock in Woocommerce

查看:92
本文介绍了产品类别过滤器显示Woocommerce中具有其库存的产品列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Im trying to create a stock report for my store which is mainly for "variable" and some "simple" products.

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

The idea is the user selects the category from a drop-down list and page refreshes showing the stock for the chosen category.

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

The problem im having seems to be with the form sending the data to the query.

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

If I manually code the slug for the category I want everything works fine for variable and simple products. however, when I try to implement the form to Post the category to the query I start getting debug errors below.

[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

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

The part I really don't understand is the fact that when i write the category in myself everything works but when the form tries to pass the same data all the errors appear?

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

Any help would be really appreciated, thanks

我的代码:

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

推荐答案

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

There are some mistakes in your code since Woocommerce 3… I have also made some little additions: - to keep the selected menu item when item on reload, - added a labeled starting option.

请尝试以下操作:

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

经过测试,可以正常工作.

Tested and works.

这篇关于产品类别过滤器显示Woocommerce中具有其库存的产品列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆